以下是文件下載單個遠程文件的實例
public static void main(String[] args) throws IOException { URL httpurl=new URL("http://yingufile-private.oss-cn-beijing.aliyuncs.com/PHYY/jpg/20170628/a85ab00c645e4b89dc38f3b8bb63a4f3"); HttpURLConnection httpConn=(HttpURLConnection)httpurl.openConnection(); httpConn.setDoOutput(true);// 使用 URL 鏈接進行輸出 httpConn.setDoInput(true);// 使用 URL 鏈接進行輸入 httpConn.setUseCaches(false);// 忽略緩存 httpConn.setRequestMethod("GET");// 設置URL請求方法 //可設置請求頭 httpConn.setRequestProperty("Content-Type", "application/octet-stream"); httpConn.setRequestProperty("Connection", "Keep-Alive");// 維持長鏈接 httpConn.setRequestProperty("Charset", "UTF-8"); //可設置請求頭 byte[] file =input2byte(httpConn.getInputStream()); writeBytesToFile(file,"D://333.png"); System.out.println(file); } public static final byte[] input2byte(InputStream inStream) throws IOException { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } byte[] in2b = swapStream.toByteArray(); return in2b; } public static File writeBytesToFile(byte[] b, String outputFile) { File file = null; FileOutputStream os = null; try { file = new File(outputFile); os = new FileOutputStream(file); os.write(b); } catch (Exception var13) { var13.printStackTrace(); } finally { try { if(os != null) { os.close(); } } catch (IOException var12) { var12.printStackTrace(); } } return file; }
遞歸壓縮方法參考:https://www.cnblogs.com/zeng1994/p/7862288.htmlhtml
整合以後代碼以下:json
String[] fids = fid.split(","); System.out.println("壓縮中..."); File zipFile = new File("C://Users//Administrator//Desktop//zipFile.zip"); //建立zip輸出流 ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFile)); //建立緩衝輸出流 BufferedOutputStream bos = new BufferedOutputStream(out); Map<String,String> mp = new HashMap<String, String>(); for (String i : fids) { Networkfile nf = networkfileService.selectById(Integer.parseInt(i)); //0移動端,1電腦 if("0".equals(ntype)){ mp.put("download_id", nf.getSf_fid()); mp.put("download_name", nf.getName()); JSONObject jsonObject = JSONObject.fromObject(mp); String dates = jsonObject.toString(); response.getWriter().print(dates); }else{ //調用打包函數 compress(out,bos,nf,nf.getName()); } } mp.put("download_id", ""); JSONObject jsonObject = JSONObject.fromObject(mp); String dates = jsonObject.toString(); response.getWriter().print(dates); bos.close(); out.close(); System.out.println("壓縮完成");
打包函數:緩存
/*文件打包zip*/ public void compress(ZipOutputStream out,BufferedOutputStream bos,Networkfile nf,String base) throws Exception { //若是路徑爲目錄(文件夾) if(nf.getType()==1)//文件夾sourceFile.isDirectory() { //取出文件夾中的文件(或子文件夾) //File[] flist = sourceFile.listFiles(); String parameters = "uid:"+nf.getUid()+",file_pid:"+nf.getId(); List<Networkstructure> listf = networkstructureService.selectAllFile(parameters); if(listf.size()==0)//若是文件夾爲空,則只需在目的地zip文件中寫入一個目錄進入點 { System.out.println(base+"/"); out.putNextEntry( new ZipEntry(base+"/") ); } else//若是文件夾不爲空,則遞歸調用compress,文件夾中的每個文件(或文件夾)進行壓縮 { for(int i=0;i<listf.size();i++) { Networkfile newnf = networkfileService.selectById(listf.get(i).getFid()); compress(out,bos,newnf,base+"/"+listf.get(i).getFile_name()); } } } else//若是不是目錄(文件夾),即爲文件,則先寫入目錄進入點,以後將文件寫入zip文件中 { URL httpurl=new URL("http://***.***.***.**:****/"+nf.getSf_fid()); HttpURLConnection httpConn=(HttpURLConnection)httpurl.openConnection(); httpConn.setDoOutput(true);// 使用 URL 鏈接進行輸出 httpConn.setDoInput(true);// 使用 URL 鏈接進行輸入 httpConn.setUseCaches(false);// 忽略緩存 httpConn.setRequestMethod("GET");// 設置URL請求方法 //可設置請求頭 httpConn.setRequestProperty("Content-Type", "application/octet-stream"); httpConn.setRequestProperty("Connection", "Keep-Alive");// 維持長鏈接 httpConn.setRequestProperty("Charset", "UTF-8"); //可設置請求頭 byte[] file =input2byte(httpConn.getInputStream()); out.putNextEntry( new ZipEntry(base) ); //將源文件寫入到zip文件中 out.write(file);//壓縮輸出 out.closeEntry(); } }