嵌入泽西文件上传的Java tomcat
我正在用tomcat嵌入式和JEST REST构建一个Java应用程序。
我需要做的是在这个应用程序内部实现一个非常简单的(和单一的)文件上传,但是我找不到任何指向我的上下文的地方......我刚刚找到了一个文件上传解决方案,但没有嵌入tomcat。
有什么建议么?
感谢大家的建议并为我的英语感到难过
如果你使用嵌入式Tomcat的Spring Boot,那么下面的代码示例会帮助你。
@Controller
public class FileUploadController {
@RequestMapping(value="/upload", method=RequestMethod.GET)
public @ResponseBody String provideUploadInfo() {
return "You can upload a file by posting to this same URL.";
}
@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)));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + "!";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
与参考。 https://spring.io/guides/gs/uploading-files/
链接地址: http://www.djcxy.com/p/91557.html上一篇: Java tomcat embedded with Jersey File Upload
下一篇: File upload with Jersey : FormDataContentDisposition is null