excel导入单元格中的文件,将文件上传到服务器


项目开发中需要导入这样的数据,根据文件路径将文件上传到文件服务器,本来以为通过File来实现,的确在本地是可以实现的。然而项目发布后,需要导入报文件不存在。原因是服务器根本不会存在这样的文件。

后来想了一个办法,将文件打成zip文件。通过解读zip文件来找到相应的文件。如图


发现使用zip导入,读取会有问题,应该里面包含了中文,造成读取不了,网上百度都是修改字符集为“GBK”,不过这种办法治标不治本,apache已经提供了一个jar来解决这个问题

 compile("org.apache.commons:commons-compress:1.8.1")

使用ZipArchiveInputStream来解决。

package net.parim.spark.unicom.provider.careercenter.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.poi.util.IOUtils;


public class UnzipFile {
	public static void main(String[] args) throws Exception{
		String path = "D:/data/content/upload/2018/05/14/jiaocai.zip";
		unzipFile(path);
		File file = new File("D:/data/content/upload/2018/05/14/jiaocai/1231.xlsx");
		System.out.println(file.getName());
//	    
//	    String filePath = path.substring(0,path.lastIndexOf("/")+1);
//	    String filePath2 = unzip(new File(path), filePath);
//	    String filePath3 = filePath2.substring(0,filePath2.lastIndexOf("\\")+1);
//	    File file = new File(filePath3);
//	    File[] files = file.listFiles();
//	    System.out.println(files[0].getAbsolutePath());
//	    for(int i=0;i<files.length;i++){
//	    	System.out.println(files[i].getAbsolutePath());
//	    }
	    //System.out.println(filePath2);
	}
    public static Map<String,Object> unzipFile(String path){
    	String filePath = "";
    	String filePath3 = "";
    	if(path.lastIndexOf("\\")!=-1){
    		filePath = path.substring(0,path.lastIndexOf("\\")+1);
    		String filePath2 = unzip(new File(path), filePath);
    	    filePath3 = filePath2.substring(0,filePath2.lastIndexOf("\\")+1);
    	}else{
    		filePath = path.substring(0,path.lastIndexOf("/")+1);
    		String filePath2 = unzip(new File(path), filePath);
    	    filePath3 = filePath2.substring(0,filePath2.lastIndexOf("/")+1);
    	}
	    File file = new File(filePath3);
	    File[] files = file.listFiles();
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("file", files[0].getAbsolutePath());
	    map.put("directory", files[1].getAbsolutePath());
	    System.out.println(map);
	    return map;
    }
	public static String unzip(File zipFile, String descDir) {
		 File directory=null;
	    try (ZipArchiveInputStream inputStream = getZipFile(zipFile)) {
	        File pathFile = new File(descDir);
	        if (!pathFile.exists()) {
	            pathFile.mkdirs();
	        }
	        ZipArchiveEntry entry = null;
	        while ((entry = inputStream.getNextZipEntry()) != null) {
	            if (entry.isDirectory()) {
	            	directory = new File(descDir, entry.getName());
	                directory.mkdirs();
	            } else {
	                OutputStream os = null;
	                try {
	                    os = new BufferedOutputStream(new FileOutputStream(new File(descDir, entry.getName())));
	                    //输出文件路径信息
	                    IOUtils.copy(inputStream, os);
	                } finally {
	                    IOUtils.closeQuietly(os);
	                }
	            }
	        }
	        final File[] files = pathFile.listFiles();
	        if (files != null && files.length == 1 && files[0].isDirectory()) {
	            // 说明只有一个文件夹
	            FileUtils.copyDirectory(files[0], pathFile);
	        }

	    } catch (Exception e) {
	    }
	    return directory.getAbsolutePath();
	}

	private static ZipArchiveInputStream getZipFile(File zipFile) throws Exception {
	    return new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值