我正在使用此代码通过 resteasy
在我的 Java 应用程序中上传文件,并且它工作得很好。
import javax.ws.rs.FormParam;
import org.jboss.resteasy.annotations.providers.multipart.PartType;
public class FileUploadForm {
public FileUploadForm() {
}
private byte[] data;
public byte[] getData() {
return data;
}
@FormParam("uploadedFile")
@PartType("application/octet-stream")
public void setData(byte[] data) {
this.data = data;
}
}
现在我想通过使用 Spring Boot 和 Spring 支架来做同样的事情。
我搜索了很多有关如何在 Spring Rest 中使用 @FormParam
和 @PartType
的信息,但没有找到任何内容。
那么我如何使用这个类来上传我的文件呢? Spring Rest 中的 @PartType
和 @FormParam
相当于什么?
最佳答案
您想在 Spring Rest 中编写文件上传代码,这很简单,您只需要使用多部分文件对象,如以下代码所示。
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
public URL uploadFileHandler(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) throws IOException {
/***Here you will get following parameters***/
System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
System.out.println("file.getContentType()" + file.getContentType());
System.out.println("file.getInputStream() " + file.getInputStream());
System.out.println("file.toString() " + file.toString());
System.out.println("file.getSize() " + file.getSize());
System.out.println("name " + name);
System.out.println("file.getBytes() " + file.getBytes());
System.out.println("file.hashCode() " + file.hashCode());
System.out.println("file.getClass() " + file.getClass());
System.out.println("file.isEmpty() " + file.isEmpty());
/***
Bussiness logic
***/
}
关于java - Spring 休息 : upload file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43591736/