文件上传工具包

好久没写博客,代码小生,
前提是需要导入相应的包,再整合spring+springmvc项目使用

package cn.zhangguimin.web.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件工上传具类
 * 
 * @Author 章先森
 * @CreateDate 2017年11月11日
 * @ www.zhngguimin.cn
 */
public class FilesUpload {

    /**
     * 上传图片方法
     * 图片格式:bmp,jpg,png,gif,jpeg,pneg
     * 最大上传大小:size 单位M
     * 上传路径:path--request.getSession().getServletContext().getRealPath("static" + File.separator + "pic");
     * @param attach
     * @param session
     * @param request
     * @return
     * @throws Exception
     */
    public String uploadPhoto(MultipartFile attach, HttpSession session, HttpServletRequest request,Integer size) throws Exception {
        String idPicPath = null;
        if (!attach.isEmpty()) {
            String path = request.getSession().getServletContext().getRealPath("static" + File.separator + "pic");
            String oldFileName = attach.getOriginalFilename();
            String prefix = FilenameUtils.getExtension(oldFileName);
            int filesize = size*1024000;
            if (attach.getSize() > filesize) {
                throw new RuntimeException("SizeError");
                // 图片格式:bmp,jpg,png,gif,jpeg,pneg
            } else if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
                    || prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")
                    || prefix.equalsIgnoreCase("gif") || prefix.equalsIgnoreCase("bmp")) {
                String fileName=System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"user."+prefix;
                File targetFile=new File(path,fileName);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }
                try {
                    attach.transferTo(targetFile);
                } catch (RuntimeException fe) {
                    fe.printStackTrace();
                }
                idPicPath=path+File.separator+fileName;
            }else {
                throw new RuntimeException("PicError");
            }
        }
        return idPicPath;
    }

    /**
     * 多文件上传
     * 文件格式:doc,xlsx,zip,txt,docx,pptx
     * 最大上传大小:size 单位M
     * 上传路径:path--request.getSession().getServletContext().getRealPath("static" + File.separator + "files");
     * @param attach
     * @param session
     * @param request
     * @return
     * @throws Exception
     */
    public List<String> uploadFiles(MultipartFile[] attachs, HttpSession session, HttpServletRequest request,Integer size) throws Exception {
        List<String> idPicPath=null;
        if (attachs.length>0) {
            idPicPath=new ArrayList<>();
            for (MultipartFile attach : attachs) {
                String path = request.getSession().getServletContext().getRealPath("static" + File.separator + "files");
                String oldFileName = attach.getOriginalFilename();
                String prefix = FilenameUtils.getExtension(oldFileName);
                int filesize = size*1024000;
                if (attach.getSize() > filesize) {
                    throw new RuntimeException("SizeError");
                    // 图片格式:doc,xlsx,zip,txt,docx,pptx
                } else if (prefix.equalsIgnoreCase("doc") || prefix.equalsIgnoreCase("docx")
                        || prefix.equalsIgnoreCase("xlsx") || prefix.equalsIgnoreCase("pptx")
                        || prefix.equalsIgnoreCase("txt") || prefix.equalsIgnoreCase("zip")) {
                    String fileName=System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"file."+prefix;
                    File targetFile=new File(path,fileName);
                    if (!targetFile.exists()) {
                        targetFile.mkdirs();
                    }
                    try {
                        attach.transferTo(targetFile);
                    } catch (RuntimeException fe) {
                        fe.printStackTrace();
                    }
                    idPicPath.add(path+File.separator+fileName);
                }else {
                    throw new RuntimeException("PicError");
                }
            }
            }
        return idPicPath;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值