zoukankan      html  css  js  c++  java
  • 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";
    }
    }
  • 相关阅读:
    网络协议 22
    网络协议 21
    网络协议 20
    网络协议 19
    网络协议 18
    网络协议 17
    网络协议 16
    网络协议 15
    网络协议 14
    .net 4.0 中的特性总结(五):并行编程
  • 原文地址:https://www.cnblogs.com/geng-geng1997/p/11358925.html
Copyright © 2011-2022 走看看