JAvaバックグラウンドでQRコードを生成してダウンロード
11076 ワード
JAvaバックグラウンドでQRコードを生成してダウンロード(バイナリストリーム形式で出力)
前提業務要求:フロントページにデータを展示し、ダウンロードボタンがあり、ダウンロードをクリックし、対応するデータのQRコードをダウンロードする.
pom.xmlファイルに依存を追加
Controllerの書き方(QRコードを生成してブラウザにストリームとして出力)
アクセス先:http://ipアドレス+ポート番号+アクセス方法のパス
次のようになります.http://192.168.0.1:8080/test
前提業務要求:フロントページにデータを展示し、ダウンロードボタンがあり、ダウンロードをクリックし、対応するデータのQRコードをダウンロードする.
pom.xmlファイルに依存を追加
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
<scope>compile</scope>
</dependency>
Controllerの書き方(QRコードを生成してブラウザにストリームとして出力)
@RestController
@RequestMapping(value = "/web")
public class webTestController {
@RequestMapping("/test")
public void dowanload(HttpServletRequest request, HttpServletResponse response) throws Exception {
//
String content = " :
:https://www.cnblogs.com/jing5464";
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
//
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// (L--7%,M--15%,Q--25%,H--30%)
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// , ( ), , ,
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
//
String fileName = " .png"
String userAgent=request.getHeader("User-Agent");
if (userAgent.contains("MSIE") || userAgent.contains("Trident") || userAgent.contains("Edge")) {//IE
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} else {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// /
}
//
response.setHeader("Content-Type","application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName );
OutputStream outputStream = response.getOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
outputStream.flush();
outputStream.close();
}
アクセス先:http://ipアドレス+ポート番号+アクセス方法のパス
次のようになります.http://192.168.0.1:8080/test