經過url下載文件

//前端發送get請求 後端直接經過url,瀏覽器下載圖片

URL url = new URL(qrcodeImageVo.getExclusiveUrl());
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + new String(getImageName().getBytes("UTF-8"), "ISO-8859-1")+Constants.JPG);

byte[] buff = new byte[2048];
BufferedInputStream is = new BufferedInputStream(url.openStream());
BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());
int bytesRead;
while (-1 != (bytesRead = is.read(buff, 0, buff.length))) {
    os.write(buff, 0, bytesRead);
}
if (is != null){
    is.close();
}
if (os != null){
    os.close();
}
相關文章
相關標籤/搜索