Java tomcat 嵌入了 Jersey 文件上传

Java tomcat embedded with Jersey File Upload

我正在构建一个 java 应用程序,其中 tomcat 嵌入了 jersey REST。

我需要做的是在此应用程序中实现一个非常简单(且单一)的文件上传,但我无法在任何地方找到适合我的上下文的指南...我只是找到了一个文件上传解决方案,但没有找到嵌入式 tomcat.

有什么建议吗?

感谢大家的建议,对不起我的英语

如果您使用 Spring Boot for Tomcat embedded,那么下面的代码示例将帮助您..

@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/