File 如何使用springs在服务器中上载文件
我是springs的新手,我想在服务器上上传文件。但是它没有以那种特定的格式保存文件..帮帮我 这是我的jsp页面File 如何使用springs在服务器中上载文件,file,upload,File,Upload,我是springs的新手,我想在服务器上上传文件。但是它没有以那种特定的格式保存文件..帮帮我 这是我的jsp页面 <html> <head> <title>Upload File Request Page</title> </head> <body> <form method="POST" action="uploadFile" enctype="multipart/form-data">
<html>
<head>
<title>Upload File Request Page</title>
</head>
<body>
<form method="POST" action="uploadFile" enctype="multipart/form-data">
File to upload: <input type="file" name="file"><br><br>
<!-- Name: <input type="text" name="file"><br> <br> -->
<input type="submit" value="Upload"> Press here to upload the file!
</form>
</body>
</html>
控制器页
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody String uploadFileHandler(@RequestParam(value = "name", required = false) String name,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
System.out.println(">>>>"+ file.getOriginalFilename());
byte[] bytes = file.getBytes();
// Creating the directory to store file
String rootPath = System.getProperty("catalina.home");
System.out.println(">"+rootPath);
File dir = new File(rootPath + File.separator + "tmpFiles/abc");
if (!dir.exists())
dir.mkdirs();
// Create the file on server
File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
logger.info("Server File Location=" + serverFile.getAbsolutePath());
return "You successfully uploaded file=" + file.getOriginalFilename();
} catch (Exception e) {
return "You failed to upload " + file.getOriginalFilename() + " => " + e.getMessage();
}
} else {
return "You failed to upload " + file.getOriginalFilename() + " because the file was empty.";
}
}
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody String uploadFileHandler(@RequestParam(value = "name", required = false) String name,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
System.out.println(">>>>"+ file.getOriginalFilename());
byte[] bytes = file.getBytes();
// Creating the directory to store file
String rootPath = System.getProperty("catalina.home");
System.out.println(">"+rootPath);
File dir = new File(rootPath + File.separator + "tmpFiles/abc");
if (!dir.exists())
dir.mkdirs();
// Create the file on server
File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
logger.info("Server File Location=" + serverFile.getAbsolutePath());
return "You successfully uploaded file=" + file.getOriginalFilename();
} catch (Exception e) {
return "You failed to upload " + file.getOriginalFilename() + " => " + e.getMessage();
}
} else {
return "You failed to upload " + file.getOriginalFilename() + " because the file was empty.";
}
}