JAVA上传文件工具类

文件上传工具类

UploadFileUtils 工具类

package com.example.newhp.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;


import org.apache.tomcat.util.http.fileupload.FileItem;
import org.springframework.web.multipart.MultipartFile;

public class UploadFileUtils {


	//  保存文件
	public synchronized static HashMap uploadFile(MultipartFile fileItem, String path, String secondPath) throws IOException {
        InputStream is=null;
        FileOutputStream fos=null;    
        HashMap mp=new HashMap();
		try {
			//上传的是文件,获得文件上传字段中的文件名
	        //注意IE或FireFox中获取的文件名是不一样的,IE中是绝对路径,FireFox中只是文件名。
	          String fileName=fileItem.getName();
	          System.out.println(fileName);
	          // 文件类型
	       /*   String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length())
	                    : null;*/
	          
	          // 文件将保存路径
	          String filePath =secondPath+getDateDir();
	            String newPath = path+filePath;
	            //文件名使用UUID和原文件名组成的
	            // 为避免原文件名存在中文,新文件名只使用UUID作为新名称
	            String newFileName= UUID.randomUUID().toString().replaceAll("-", "")+".pdf";
              
	            // 判断路径是否存在,如果不存在就创建一个
	            File filepath = new File(newPath, newFileName);
	            if (!filepath.getParentFile().exists())
	                filepath.getParentFile().mkdirs();
	            	
	    	  is=fileItem.getInputStream();
	          fos=new FileOutputStream(newPath+newFileName);
	          byte[] buff=new byte[1024];
	          int len=0;
	          while((len=is.read(buff))>0){
	              fos.write(buff);
	          }
	          
	          mp.put("fileName",newFileName);
	          mp.put("filePath",filePath);
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
		      is.close();
              fos.close();
		}	
		return mp;
	}
	
	/**
     * 生成以时间作为文件夹路径
     * @return
     */
    public static String getDateDir() {
    	SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间 
        sdf.applyPattern("yyyyMM/dd/");// a为am/pm的标记  
        Date date = new Date();// 获取当前时间 
        return sdf.format(date);
    }
    
    

	/**
	 * 更换pdf报告
	 * @return
	 * @zpath=(更换文件,路径,盘符+文件夹+文件名)
	 */
	public static Integer ghFile(MultipartFile file,String zpath) {
		BufferedInputStream in=null;
		BufferedOutputStream out=null;
		System.err.println(zpath);
		try {
			File fileold = new File(zpath);
			if(fileold.exists()){
				fileold.delete();
			}

			in = new BufferedInputStream(file.getInputStream());
			// 获取输出流
			out = new BufferedOutputStream(new FileOutputStream(zpath));

			// 读数据
			byte[] bb = new byte[1024];// 用来存储每次读取到的字节数组
			int n;// 每次读取到的字节数组的长度
			while ((n = in.read(bb)) != -1) {
				out.write(bb, 0, n);// 写入到输出流
			}
			out.flush();
			out.close();// 关闭流
			in.close();
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值