FileUpload
博客专区 > Curry_M 的博客 > 博客详情
FileUpload
Curry_M 发表于8个月前
FileUpload
  • 发表于 8个月前
  • 阅读 5
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

package com.pingan.common.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadUtils {

    @SuppressWarnings("unchecked")
	public static List<FileItem> getFileList(HttpServletRequest request) {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> items = new ArrayList<FileItem>();
        try {
            items = upload.parseRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return items;
    }

    /**
     * 主要用于从影像系统读出字符流写入响应进行下载
     * @param response
     * @param url
     * @param fileName
     * @throws IOException
     */
    public static void readContentFromIM(HttpServletResponse response, String url, String fileName) throws IOException {
        // Post请求的url,与get不同的是不需要带参数 
        URL postUrl = new URL(url);
        // 打开连接 
        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.connect();
        /* String excel2010 = "D:\\test.png";
         FileInputStream inputStream = new FileInputStream(new File(excel2010));
         BufferedInputStream reader = new BufferedInputStream(inputStream);*/
        BufferedInputStream reader = new BufferedInputStream(connection.getInputStream());

        // 设置输出的格式
        response.reset();
        response.setContentType("application/octet-stream");//application/x-msdownload
        response.addHeader("Content-Disposition",
                "attachment; filename=\"" + java.net.URLEncoder.encode(fileName, "UTF-8") + "\"");
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = reader.read(b)) > 0) {
                response.getOutputStream().write(b, 0, len);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 主要用于读nas上的模板
     * @param filePath
     * @param fileName
     * @return
     * @throws IOException
     */
    public static byte[] readContentFromNas(String fileName) throws IOException {
        InputStream inStream = new FileInputStream(fileName);// 文件的存放路径
        BufferedInputStream reader = new BufferedInputStream(inStream);
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据 
        int rc = 0;
        while ((rc = reader.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        reader.close();
        byte[] data = swapStream.toByteArray(); //in_b为转换之后的结果 
        swapStream.close();
        return data;
    }

}

 

共有 人打赏支持
粉丝 2
博文 38
码字总数 40117
×
Curry_M
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: