如何将多个json文件和表单数据上传到休息服务

use*_*026 7 rest json jersey-2.0

我想上传多个JSON文件(如学生成绩JSON,学生课程安排JSON,学生作业JSON等)和元数据(如学生信息)
To Rest服务在Jersy和tomcat上运行

在这里采取什么方法?它应该像一个控制器吗?是否可以指定上传的JSOn结构?如果其中一个文件丢失怎么办?

@Path("/submitStudentInformation")
public class SubmitStudInfoController {
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces("text/plain")
    @Path("/multipleFiles")
public Response uploadFiles(@Context HttpServletRequest request) {
Run Code Online (Sandbox Code Playgroud)

小智 3

在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();
     }
} 
Run Code Online (Sandbox Code Playgroud)

前端我使用的是 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]);
        }
    });
};
Run Code Online (Sandbox Code Playgroud)