如下是文件下载单个远程文件的实例

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.html

整合之后代码如下:

          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();
        }
    }

 

posted on 2019-05-29 11:06  茫无所知  阅读(626)  评论(0编辑  收藏  举报