• Spring MVC 上传文件---代码实现


    package cn.kgc.updatefile;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.multipart.MultipartFile;

    import javax.servlet.http.HttpServletRequest;
    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;

    @Controller
    @RequestMapping("/user")
    public class UpdateFile {

    @RequestMapping("/uploadTest")
    public String uploadTest(String username, MultipartFile upload, HttpServletRequest request){
    String realPath = request.getSession().getServletContext().getRealPath("/updateTest");
    File file = new File(realPath);
    if(!file.exists()){
    file.mkdirs();
    }
    String uuidName = UUID.randomUUID().toString().replace("-", "");
    String originalFilename = upload.getOriginalFilename();
    System.out.println(originalFilename);

    String fileName=uuidName+originalFilename;
    System.out.println(fileName);

    try {
    upload.transferTo(new File(file,fileName));
    } catch (IOException e) {
    e.printStackTrace();
    }
    return "show";
    }
    }
  • 相关阅读:
    部分类Partial
    Ajax之XMLHttpRequest
    C#结构(Struct)
    sql数据管理语句
    mysql基本语句
    bootstrap插件-滚动监听
    一轮项目学习总结
    js滚动监听
    Ajax
    各框架、插件记录
  • 原文地址:https://www.cnblogs.com/geng-geng1997/p/11358925.html
Copyright © 2020-2023  润新知