java 网络文件下载(并命中文名)
public void download(HttpServletRequest request, HttpServletResponse response){ //获取服务器文件 String file_url = "http//:www.baidu.com/201811239413.doc"; InputStream ins = null; try { ins = new URL(file_url).openStream(); /* 设置文件ContentType类型,这样设置,会自动判断下载文件类型 */ response.setContentType("multipart/form-data"); /* 设置文件头:最后一个参数是设置下载文件名 */ response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode("江苏南京.doc","UTF-8"));//适应中文名称 OutputStream os = response.getOutputStream(); byte[] b = new byte[1024]; int len; while((len = ins.read(b)) > 0){ os.write(b,0,len); } os.flush(); os.close(); ins.close(); } catch (FileNotFoundException e) { System.out.println("没有该文件"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }