在Java中上载excel文件时,如何修复错误415不支持的媒体类型错误?

在Java中上载excel文件时,如何修复错误415不支持的媒体类型错误?,java,multipartform-data,dropzone.js,Java,Multipartform Data,Dropzone.js,使用dropzone.js将excel文件上载到使用Java的后端服务器时出现问题。尝试上载文件时出现415错误 这是我处理上传的excel文件的代码 @Path("/import/customers") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) @POST public Response importCustomersExcel(InMultiPart inMP) throws

使用dropzone.js将excel文件上载到使用Java的后端服务器时出现问题。尝试上载文件时出现415错误

这是我处理上传的excel文件的代码

@Path("/import/customers")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@POST
public Response importCustomersExcel(InMultiPart inMP) throws IOException, JSONException {

    System.out.println("PUT /import/customers");

    CustomerService customerService = Container.getCustomerService();
    FileProcessing processing = new FileProcessing();

    Response response = null;
    File retailersFile = null;

    boolean validationError = false;

    try {
        retailersFile = processing.toFile(inMP);
        customerService.validateImportExcel(retailersFile);
    } catch (ValidationException e) {
        validationError = true;
        String errorMessage = e.getMessage();
        if (e.getErrors() != null && !e.getErrors().isEmpty()) {
            ExcelRowErrorMessageBuilder messageBuilder = new ExcelRowErrorMessageBuilder(e.getErrors());
            errorMessage += messageBuilder.generateErrorMessage();
        }
        JSONObject json = new JSONObject();
        json.put("error", errorMessage);
        response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(json).build();
        e.printStackTrace();
    } catch (Exception e) {
        validationError = true;
        JSONObject json = new JSONObject();
        json.put("error", e.getMessage());
        response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(json).build();
        e.printStackTrace();
    } finally {
        //delete the file if error
        //else THE file is needed
        if (validationError) {
            boolean fileDeleted = FileProcessing.tryDelete(retailersFile);
            System.err.println("File " + (retailersFile == null ? "" :retailersFile.getName() + " was deleted : " + fileDeleted));
        }
    }

    if (!validationError) {

        boolean isImportRunning = false;
        synchronized (customerService) {
            isImportRunning = customerService.isRetailersImportRunning();

            if (!isImportRunning) {
                customerService.setImportRunning(true);
            }
        }

        if (!isImportRunning) {
            CustomerImportRunner customerImportRunner = new CustomerImportRunner(customerService, retailersFile);
            Thread thread = new Thread(customerImportRunner);
            thread.start();
            JSONObject json = new JSONObject();
            json.put("info", "Import started successfully!");
            response = Response.status(Status.OK).entity(json).build();
        } else {
            JSONObject json = new JSONObject();
            json.put("error", "Import is already running");
            response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(json).build();
        }
    }

    return response;
}
这是我的dropzone.js文件上传表

<form action="admin/rest/import/customers" class="dropzone" enctype="multipart/form-data"></form>

我已经故意在我的表单中添加了多部分/表单数据,但是当我在上传时检查流程时,内容类型是
content-type:multipart/form-data;边界=----WebKitFormBoundaryG1ZnT8kA25wpWLdW

. 上传excel文件时如何修复415错误,它显示了415错误。我错过了什么来添加Java代码?请帮忙。谢谢。

对于Jersey,您必须做两件事:

  • 添加
    寄存器(MultiPartFeature.class)到您的
    ResourceConfig
  • 将您的方法
    importCustomersExcel(InMultiPart inMP)
    更改为
    importcustomersecel(@FormDataParam(“文件”)InputStream流,@FormDataParam(“文件”)formdatacontentdispositionfiledetail)

  • 然后,您可以通过
    stream
    访问文件内容,并通过
    fileDetail
    访问文件详细信息。还要确保您的Dropzone参数名是
    文件

    尝试更改您的方法参数以匹配此处的参数:@HalkoKarr Sajtarevic,我的方法哪里出错了?哦-刚刚看到您使用的是jersey而不是spring-对不起,我的错误!在何处添加寄存器(MultiPartFeature.class);您必须将其放入类的构造函数中,该类正在扩展
    ResourceConfig
    谢谢。在这种情况下,我仍然在学习Java和所有东西。再次感谢。