上传文件或图片 controller层

该博客主要展示了如何使用Spring MVC实现文件上传功能。在`UploadController`类中,定义了两个请求处理方法:`up`和`upload`。`up`方法用于处理单一文件上传,调用`uploadService`进行保存;`upload`方法处理多文件上传,遍历`MultipartHttpServletRequest`获取所有文件并分别保存。每个文件保存时,通过UUID生成新的文件名,并保存到指定目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/file")
public class UploadController {

   private static final Logger log = LoggerFactory.getLogger(UploadController.class);

   @Autowired
   private FileUploadService uploadService;

   @RequestMapping("up")
   @ResponseBody
   public void up(@RequestParam MultipartFile file){

      // 1. uploadService.canupload  判断 能否保存文件

      // 2.

      uploadService.saveFile(FileTypeEnum.FILE_GUARANTEE,file,1L,1L);


   }

   @RequestMapping("/upload")
   @ResponseBody
   public JsonResult upload(DefaultMultipartHttpServletRequest multipartRequest, HttpServletRequest request) {
      JsonResult json = new JsonResult();
      json.setSuccess(true);
      if (multipartRequest != null) {
         Iterator<String> iterator = multipartRequest.getFileNames();
         while (iterator.hasNext()) {
            MultipartFile file = multipartRequest.getFile((String) iterator.next());
            if (!file.isEmpty()) {
               log.debug("获取文件MIME类型-" + file.getContentType());// 获取文件MIME类型

               log.debug("获取表单中文件组件的名字-" + file.getName());// 获取表单中文件组件的名字

               log.debug("获取上传文件的原名-" + file.getOriginalFilename());// 获取上传文件的原名

               log.debug("获取文件的字节大小,单位byte-" + file.getSize());// 获取文件的字节大小,单位byte

               String fileName = UUID.randomUUID().toString().replaceAll("-", "")
                     + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));// 保存后的文件名
               log.info(fileName);

               try {
                  // 文件保存路径
                  String filePath = request.getServletContext().getRealPath("/") + "attached" + File.separator;
                  json.setMsg("上传成功,请查看【" + filePath + "】目录");
                  File uploadFile = new File(filePath + fileName);
                  uploadFile.mkdirs();
                  file.transferTo(uploadFile);// 保存到一个目标文件中。
                  Map<String, Object> m = new HashMap<String, Object>();
                  m.put("fileUrl", "/attached/" + fileName);
                  json.setObj(m);
               } catch (Exception e) {
                  json.setSuccess(false);
                  json.setMsg(e.getLocalizedMessage());
                  e.printStackTrace();
               }
            }
         }
      }
      return json;
   }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值