我尝试使用Spring Boot上传一些文件。 有一个问题,我应该指向哪个路径来存储我的文件。 您可以在下面看到我的UploadController、模板和Application类。
@Controller
public class FileUploadController {
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
这是我的表单。
<div class="container">
<form method="POST" enctype="multipart/form-data"
action="/upload">
File to upload:
<input type="file" name="file"/><br/> Name:
<input
type="text" name="name"/><br/> <br/> <input type="submit"
value="Upload"/> Press here to upload the file!
</form>
</div>
这是一个应用程序类。
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@Bean
MultipartConfigElement multipartConfigElement() {
MultiPartConfigFactory factory = new MultiPartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
return factory.createMultipartConfig();
}
}