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";
}
}
转载于:https://www.cnblogs.com/geng-geng1997/p/11358925.html