java文件上传控制器

@RestController
@RequestMapping(value = "/uploadfile")
public class UpLoadFileController {

    private static Logger logger = LoggerFactory.getLogger(UpLoadFileController.class);

    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/image/upload")
    public ResultModel<Map<String,Object>> imgUpload(@RequestParam("imgFile")MultipartFile file) throws Exception{
       Map<String, Object> map = new HashMap<String, Object>();
       String fileName = file.getOriginalFilename();
       String uuid = UuidUtil.getUuid();
       String filePath = "files/img/"+ DateTimeUtil.getNowDateStr(DateTimeUtil.yyyy_MM_dd)+ "/"+uuid+"/";
       String rearllyfilePath = CommonConstant.FILE_ROOT_PATH + filePath;
       
       //获取文件后缀名
        String [] lastName = fileName.split("\\.");
        if(lastName.length<2){
            throw new MyException("无法获取文件后缀名!请重试!");
        }
       if(Common.isnull(file) || file.isEmpty()){
          throw new MyException("无法读取图片请重试!");
        }
       //大于2M不能保存
        if(file.getSize()>10240000){
            throw new MyException("图片大于10M无法上传!");
        }
        if(!fileName.endsWith(".jpg") && !fileName.endsWith(".png") && !fileName.endsWith(".gif")&& !fileName.endsWith(".svg")&& !fileName.endsWith(".jpeg")) {
           throw new MyException("图片上传失败!请上传jpg、png、jpeg、svg、gif格式的图片!");
        }
        //返回
        map.put("image", filePath+fileName);
        map.put("fileName", fileName);
        map.put("saveAddress", rearllyfilePath);
        try {
            uploadFile(file.getBytes(), rearllyfilePath, fileName);
        } catch (Exception e) {
            logger.error("图片上传失败!原因={}",e.getMessage());
            throw new MyException("图片上传失败!");
        }
       return new ResultModel<Map<String,Object>>(map);
    }
    /**
     * 上传文件
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/file/upload")
    public ResultModel<Map<String,Object>> fileUpload(@RequestParam("file")MultipartFile file) throws Exception{
       Map<String, Object> map = new HashMap<String, Object>();
       String fileName = file.getOriginalFilename();
       String uuid = UuidUtil.getUuid();
       String filePath = "files/file/"+ DateTimeUtil.getNowDateStr(DateTimeUtil.yyyy_MM_dd)+ "/"+uuid+"/";
       String rearllyfilePath  = CommonConstant.FILE_ROOT_PATH + filePath;
       //获取文件后缀名
        String [] lastName = fileName.split("\\.");
       if(Common.isnull(file) || file.isEmpty()){
          throw new MyException("无法读取文件请重试!");
        }
       //大于10M不能保存
        if(file.getSize()>1024*10240){
            throw new MyException("文件大小超过10M无法上传!");
        }
        if(Common.isnull(lastName) || Common.isnull(lastName[lastName.length-1])){
           throw new MyException("非法文件,无法上传!");
        }
        if(CommonConstant.FILE_END_NAMES.indexOf(lastName[lastName.length-1].toLowerCase())==-1) {
           throw new MyException("非法文件,无法上传!");
        }
        //返回
        map.put("url", filePath+fileName);
        map.put("fileName", fileName);
        map.put("saveAddress", rearllyfilePath);
        try {
            uploadFile(file.getBytes(), rearllyfilePath, fileName);
        } catch (Exception e) {
            logger.error("文件上传失败!原因={}",e.getMessage());
            throw new MyException("文件上传失败!");
        }
       return new ResultModel<Map<String,Object>>(map);
    }
    /**
     * 新建文件夹,保存文件
     * @param file
     * @param filePath
     * @param fileName
     * @throws Exception
     */
    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception{
        //设置文件路径
       File targetFile = new File(filePath);
       //判断路径是否已经存在 如果不存在就新建
       if(!targetFile.exists()) {
          targetFile.mkdirs();
       }
       //写入文件
       FileOutputStream out = new FileOutputStream(filePath + fileName);
       out.write(file);
       out.flush();
       out.close();
    }


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值