@RequestMapping("/download") @ResponseBody public void downloadImg(String imgUrl,String hospitalName, HttpServletResponse response){ try { // 构造URL URL url = new URL(imgUrl); // 打开连接 URLConnection con = url.openConnection(); // 设置请求超时为5s con.setConnectTimeout(5 * 1000); // 输入流 InputStream is = con.getInputStream(); // 1K的数据缓冲 byte[] bs = new byte[1024]; // 读取到的数据长度 int len; // 输出数据流 ByteArrayOutputStream output = new ByteArrayOutputStream(); // 开始读取 while ((len = is.read(bs)) != -1) { output.write(bs, 0, len); } response.reset(); response.setHeader("Content-Disposition", "attachment; filename="+ new String(hospitalName.trim().getBytes("utf-8"), "iso8859-1")); response.addHeader("Content-Length", "" + output.toByteArray().length); response.setContentType("application/octet-stream; charset=UTF-8"); IOUtils.write(output.toByteArray(), response.getOutputStream()); output.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)