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



使用 dropzone.js 将 excel 文件上传到我的后端服务器时遇到问题。当我尝试上传文件时出现 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; boundary=----WebKitFormBoundaryG1ZnT8kA25wpWLdW.上传 excel 文件时如何修复 415 错误,它显示 415 错误。我错过了什么放在我的 Java 代码上?请帮忙。谢谢。

对于泽西岛,您必须做两件事:

  1. register(MultiPartFeature.class);添加到您的ResourceConfig
  2. 将方法importCustomersExcel(InMultiPart inMP)更改为importCustomersExcel(@FormDataParam("file") InputStream stream, @FormDataParam("file") FormDataContentDisposition fileDetail)

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

最新更新