public String downLoadFileContent() throws IOException{ // 下載網絡文件 int bytesum = 0; int byteread = 0; response = CustomerUtil.getResponse(); request = CustomerUtil.getRequest(); String filePath = request.getParameter("filePath");//http://localhost:8080/mypro/img String fileName = request.getParameter("fileName");//201212121212.txt URL url = new URL(filePath+fileName); try { URLConnection conn = url.openConnection(); InputStream inStream = conn.getInputStream(); OutputStream fs=response.getOutputStream(); //設置response的編碼方式 response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("gbk"),"iso-8859-1")); response.setHeader("Content-Disposition","attachment;filename="+fileName); byte[] buffer = new byte[1204]; int length; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; fs.write(buffer, 0, byteread); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
java 下載文件功能
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.