我想上传多个 JSON 文件(如学生成绩 JSON、学生类(class)安排 JSON、学生作业 JSON 等)和元数据(如学生信息)
在 Jersy 和 tomcat 上运行的 Rest 服务
在这里采取什么方法?它应该像一个 Controller 吗?是否可以指定上传的 JSOn 结构?
如果其中一个文件丢失怎么办?
@Path("/submitStudentInformation")
public class SubmitStudInfoController {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/plain")
@Path("/multipleFiles")
public Response uploadFiles(@Context HttpServletRequest request) {
最佳答案
在rest api中发送文件列表
@POST
@Path("/uploadFile")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(@FormDataParam("files") List<FormDataBodyPart> files)
if(files!=null) {
for (int i = 0; i < files.size(); i++) {
FormDataBodyPart this_formDataBodyPartFile = files.get(i);
ContentDisposition this_contentDispositionHeader = this_formDataBodyPartFile.getContentDisposition();
InputStream this_fileInputStream = this_formDataBodyPartFile.getValueAs(InputStream.class);
FormDataContentDisposition fileDetail = (FormDataContentDisposition) this_contentDispositionHeader;
String imagename = fileDetail.getFileName();
}
}
前端我使用 angularjs,所以我在 formdata 中设置了多个文件
var formdata = new FormData();
$scope.getTheFiles = function(element) {
$scope.$apply(function($scope) {
$scope.files = element.files;
for (var i = 0; i < element.files.length; i++) {
formdata.append('files', element.files[i]);
}
});
};
关于json - 如何上传多个json文件和表单数据到rest服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46267607/