前臺html代碼展示:
<div class="col-xs-11 post-control">
<button class="btn btn-primary" id="userSavePic" type="button" onclick="savePiceture()"><i class="fa fa-save"></i>二維碼下載</button>
</div>
<script type="text/javascript">
function savePiceture(){
console.log("1");
var url ="/areaExt/downloadQrCode";
window.open("<%=request.getContextPath()%>/areaExt/downloadQrCode");
}
</script>
後端Java代碼:
@RequestMapping("/downloadQrCode")
@ResponseBody
public void dowanload(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception {
UserBean userBean = (UserBean) session.getAttribute(SessionDefine.USER);
String areaCode = userBean.getDefaultArea().getAreaCode();
String areaName = userBean.getDefaultArea().getAreaName();
//生成圖片二維碼========================================webSite+"esc/index?zmbh="+1+"&v="+System.currentTimeMillis()
String imgStr = qrService.createQrCode(webSite+"/html5/jqsq.html?sqbm="+areaCode);
BASE64Decoder decoder = new BASE64Decoder();
InputStream input = new ByteArrayInputStream(decoder.decodeBuffer(imgStr));
Image src = ImageIO.read(input);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(400, 450,BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, 400, 400, null);
//帶文字
//計算文字開始的位置
//x開始的位置:(圖片寬度-字體大小*字的個數)/2
g.setColor(new Color(255,255,255));
int startX = (width-(20*areaName.length()))/2+50;
//y開始的位置:圖片高度-(圖片高度-圖片寬度)/2
int startY = 435;
Font font=new Font(null, Font.BOLD, 30);
g.setFont(font);
g.drawString(areaName+"結親二維碼", startX, startY);
//
g.dispose();
//=====================================================
//步驟一:BufferedImage 轉 InputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(image, "png", imageOutput);
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
//步驟二:獲得文件長度
long length = imageOutput.length();
try {
// 文件名
String fileName = "二維碼信息-" + TimeUtil.getTime(TimeUtil.getNow(), "yyyy-MM-dd")+".png";
//設置文件長度
response.setContentLength((int)length);
//步驟三:傳入文件名、輸入流、響應
fileDownload(fileName,inputStream,response);
} catch (Exception e) {
e.printStackTrace();
}
}
//文件下載方法,工具類
public static void fileDownload(String filename, InputStream input, HttpServletResponse response){
try {
byte[] buffer = new byte[input.available()];
input.read(buffer);
input.close();
// 清空response
response.reset();
// 設置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO-8859-1"));
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
//關閉,即下載
toClient.close();
}catch (Exception e){
e.printStackTrace();
}
}
工具類:
//文件下載方法,工具類
public static void fileDownload(String filename, InputStream input, HttpServletResponse response){
try {
byte[] buffer = new byte[input.available()];
input.read(buffer);
input.close();
// 清空response
response.reset();
// 設置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO-8859-1"));
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
//關閉,即下載
toClient.close();
}catch (Exception e){
e.printStackTrace();
}
}