Java 下载 (网络资源、本地资源)

mac2024-04-10  28

**

1.通过url来下载网络资源

**

//下载网络资源 @RequestMapping("downloadNet") @ResponseBody public void downloadNet(HttpServletResponse response,String zlmc,String cclj) throws MalformedURLException { // 下载网络文件 int bytesum = 0; int byteread = 0; try { URL url = new URL(cclj);//http://192.168.1.250:8091/xxx/xx.xx HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //设置超时间为3秒 conn.setConnectTimeout(3*1000); //防止屏蔽程序抓取而返回403错误 conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //得到输入流 InputStream inputStream = conn.getInputStream(); //获取自己数组 byte[] buffer = readInputStream(inputStream); // 清空response response.reset(); // 设置response的Header response.addHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(zlmc, "UTF-8")); response.setCharacterEncoding("UTF-8"); response.addHeader("Content-Length", "" + buffer.length); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 从输入流中获取字节数组 * @param inputStream * @return * @throws IOException */ public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return bos.toByteArray(); }

2.通过指定本地路径下载文件

1)这种方式是通过寻找项目中静态资源中 或者电脑中某个路径下 的文件
/** * 下载指定下的文件 * @param response * @param filemc 文件名称 * @param filesuffix 文件后缀 * @throws IOException */ @RequestMapping("/xzmb/{filemc}/{filesuffix}") public void downloadFile(HttpServletRequest request,HttpServletResponse response,@PathVariable("filemc") String filemc,@PathVariable("filesuffix") String filesuffix) throws IOException { //文件路径 String excelPath = request.getSession().getServletContext().getRealPath("/resources/DownTemplate/"+filemc+"."+filesuffix); File file = new File(excelPath); //文件名 String filename = file.getName(); InputStream inputStream = new FileInputStream(file); //强制下载不打开 response.setContentType("application/force-download"); OutputStream out = response.getOutputStream(); //使用URLEncoder来防止文件名乱码或者读取错误 response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); int b = 0; byte[] buffer = new byte[1000000]; while (b != -1) { b = inputStream.read(buffer); if (b != -1) out.write(buffer, 0, b); } System.out.println(buffer); inputStream.close(); out.close(); out.flush(); }

js调用

window.open("/xzmb/zhlr/xlsx");
2)这种方式是通过查找src下或者resources的文件
/** * 下载指定下的文件 * @param response * @param wjmc文件名称 * @throws IOException */ @RequestMapping("/xzmb/{filemc}}") public void downloadFile(HttpServletRequest request,HttpServletResponse response,@PathVariable("wjmc") String wjmc) throws IOException { try { Resource resource = (Resource) new ClassPathResource("resources/DownTemplate/"+wjmc); File file = ((AbstractFileResolvingResource) resource).getFile(); String filename = ((AbstractResource) resource).getFilename(); InputStream inputStream = new FileInputStream(file); response.setContentType("application/force-download"); OutputStream out = response.getOutputStream(); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); int b = 0; byte[] buffer = new byte[1000000]; while (b != -1) { b = inputStream.read(buffer); if (b != -1) out.write(buffer, 0, b); } System.out.println(buffer); inputStream.close(); out.close(); out.flush(); } catch (IOException e) { e.printStackTrace(); } }
最新回复(0)