java 網絡文件下載(並命中文名)
阿新 • • 發佈:2018-12-03
close malformed pos encoding download let sys tput not
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(); } }
java 網絡文件下載(並命中文名)