src/main/java/cn/pzhu/pserson/controller/CommonController.java

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

package cn.pzhu.pserson.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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 javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class CommonController {

  @RequestMapping(value = "/{formName}")
  public String loginForm(@PathVariable String formName) {
    // 作为一个空方法,可以匹配任何无效输入,再跳转到404
    return formName;
//		String blank = "blank";
//		return blank;
  }

//  @RequestMapping(value = "/")
//  public String index() {
//    String blank = "index";
//    return blank;
//  }

  @RequestMapping(value = "/welcome")
  public String welcome() {
    String blank = "welcome";
    return blank;
  }

  @RequestMapping("/common/upload")
  @ResponseBody
  public Map<String, Object> upload(HttpServletRequest servletRequest, @RequestParam("file") MultipartFile file) throws IOException {
    //如果文件内容不为空,则写入上传路径
    if (!file.isEmpty()) {
      //上传文件路径
      String filePath = servletRequest.getSession().getServletContext().getRealPath("/images");
      // 获取原始图片的扩展名
      String originalFilename = file.getOriginalFilename();
      // 生成文件新的名字
      String newFileName = UUID.randomUUID() + originalFilename;
      // 封装上传文件位置的全路径
      File targetFile = new File(new File(filePath).getAbsolutePath()+ "/" + newFileName);
      if (!targetFile.getParentFile().exists()) {
        targetFile.getParentFile().mkdirs();
      }
      file.transferTo(targetFile);
      Map<String, Object> res = new HashMap<>();
      //返回的是一个url对象
      res.put("url", "/images/" + newFileName);
      res.put("code", "200");
后面还有 6 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。