我嘗試上傳一些文件,使用Spring Boot。 還有一個問題,我應該在哪裏指出我的文件將被存儲的路徑。 你可以看到我的UploadController,模板和應用程序類在下面。春季開機上傳文件路徑
@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();
}
}
很顯然,我的意思是我應該在哪裏寫路徑這些文件。 – user3127896
對不起,我沒有得到。你發佈的哪部分代碼不起作用,或者讓你不快樂? –