经过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();
}
相关文章
相关标签/搜索