java大文件下载,工具类DiskFileItem

Posted 500年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java大文件下载,工具类DiskFileItem相关的知识,希望对你有一定的参考价值。

java大文件下载,工具类DiskFileItem

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.webber.cm.util.X509TrustUtil;

public class Test 

	public String uploadSource(String path,String fileName,String tempFilePath)
        InputStream is = null;
        OutputStream os = null;
        String mimeType;
        try 
            if (path.startsWith("https://")) 
                SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
                sslcontext.init(null, new TrustManager[] new X509TrustUtil(), new java.security.SecureRandom());
                URL url= new URL(null, path, new sun.net.www.protocol.https.Handler());
                HostnameVerifier ignoreHostnameVerifier = (s, sslSession) -> true;
                HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
                HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
                HttpsURLConnection urlCon = (HttpsURLConnection)url.openConnection();
                urlCon.setHostnameVerifier(ignoreHostnameVerifier);
                urlCon.setSSLSocketFactory(sslcontext.getSocketFactory());
                urlCon.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                urlCon.setConnectTimeout(60000);
                urlCon.setReadTimeout(60000);
                urlCon.setDoOutput(true);
                urlCon.setDoInput(true);
                urlCon.setUseCaches(false);
                // 设置请求方式(GET/POST)
                urlCon.setRequestMethod("GET");
                System.out.println(urlCon.getResponseMessage());
                int code = urlCon.getResponseCode();
                if (code != HttpURLConnection.HTTP_OK) 
                    throw new Exception("文件读取失败");
                
                is = urlCon.getInputStream();
                mimeType = urlCon.getContentType();
             else 
                URL url = new URL(path);
                URLConnection connection = url.openConnection();
                is = connection.getInputStream();
                // "image/jpeg"
                mimeType = connection.getContentType();
            
            FileItem fileItem = new DiskFileItem("file", mimeType, true,
                    fileName, 1024 * 1024 * 3, new File(tempFilePath));
            os = fileItem.getOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) > -1) 
                os.write(buffer, 0, len);
            
            os.flush();

            MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
            //String fileInfo = storageService.uploadFile(multipartFile);
            return  "ok";
         catch (Exception e) 
            e.printStackTrace();
         finally 
            try 
                if(is != null)
                    is.close();
                
             catch (IOException e) 
                e.printStackTrace();
            
            try 
                if(os != null)
                    os.close();
                
             catch (IOException e) 
                e.printStackTrace();
            
        
        return  null;
    


以上是关于java大文件下载,工具类DiskFileItem的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程系列:CountDownLatchSemaphore等4大并发工具类详解

大数据必学Java基础(二十八):Arrays工具类和数组的复制操作

JUC 常用 4 大并发工具类

Java并发工具合集JUC大爆发!!!

Java web 大数据量数据库操作批处理工具类

JUC 常用 4 大并发工具类是哪几个?(面试必问)