这个问题可能对大家来说都是愚蠢的,但是我从1周开始就想办法解决我的spring-boot应用程序中的一个问题。
我开发了一个API来读取由许多客户端发送的多部分文件。
我通过邮递员测试了api,从而获得了预期的结果,但是当我从客户端调用API时,它甚至没有到达spring boot控制器。
这是我的代码在springboot中的样子
@RestController
@RequestMapping("/v2")
public class PDFExtractController {
private static final long serialVersionUID = 1L;
@RequestMapping(value = "/document/{DocType}",method =RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes =MediaType.MULTIPART_FORM_DATA_VALUE )
public ResponseEntity fileUpload(@PathVariable("DocType") String docType, @RequestParam("pdf") MultipartFile multipartfiles, @RequestParam(name ="provider", required=false) String AcceptProvider, @RequestParam(name= "verbose", required=false) String AcceptVerbose,@RequestParam(name= "pdf_password",required=false) String AcceptPass, HttpServletRequest request){
boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
String provider = null;
String password = null;
boolean verbose= false;
try{
MultipartFile uploadedFile = multipartfiles;
if(uploadedFile!=null){
String fieldname = uploadedFile.getName();
if(AcceptProvider!=null){
provider=AcceptProvider.trim();
SplunkMgr.addtoMDC(MDCFieldNames.PROVIDER.getValue(), provider.trim());
}
if(AcceptVerbose!=null){
verbose = "true".equals(AcceptVerbose.trim());
}
if (PESConstants.PARAM_PDF.equals(fieldname)) {
file = uploadedFile.getBytes();
}
if(AcceptPass!=null){
password=AcceptPass.trim();
SplunkMgr.addtoMDC(MDCFieldNames.PASSWORD_PROVIDED.getValue(), String.valueOf(password != null));
}
}
}// Network problem
catch (IOException e) {
log.error("Network error when receiving request.", e);
PDFExtractResult pdfExtractResult = new PDFExtractResult().markInError(PdfExtractReferenceMgr.instance().getMessage(MessageConstants.InvitcoMsgCode.PES_NETWORK_ERROR));
responseData = PdfServletMgr.createResponseJson(pdfExtractResult, verbose);
}
//---here is my code to process the file and sending the response----
}
但是我的客户发送的邮件如下
HttpURLConnection connection = (HttpURLConnection)new
URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type",
"form-data; boundary=" + boundary);
SplunkMgr.instance().addSplunkMessage("Calling PES Service with
userId = " + userId, LogLevel.INFO);
}
else{
try {
SplunkMgr.instance().addSplunkMessage("Could not get user context from logged in user " , LogLevel.INFO);
new MappingSysAdminServiceImpl().logout();
} catch (SAMException e) {
SplunkMgr.instance().addSplunkMessage("Logout Failed - Unexpected error when logging out. " + (e !=null ? e.getMessage() : e), LogLevel.ERROR);
}
}
if(autoMapper!=null)
// here header data setting
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, CHARSET), false);
// Add params
if (providerId != null && providerId.length() > 0) {
addParam(writer, boundary, PESConstants.PARAM_PROVIDER, providerId);
}
if (verbose) {
addParam(writer, boundary, PESConstants.PARAM_VERBOSE, "true");
}
if (password != null && password.length() > 0) {
addParam(writer, boundary, PESConstants.PARAM_PASSWORD, password);
}
// Send file
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: multipart/form-data; name=\""+PESConstants.PARAM_PDF+"\"").append(CRLF);
writer.append("Content-Type: application/pdf").append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
output.write(pdfFile);
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF).flush();
// Read response
InputStream input = null;
您的帮助确实可以帮助我进军。
注意:如果我使用springboot multipart,则可以无障碍运行并获得输出。问题是当我从客户端打来时...
请建议我必须做些什么更改才能继续进行
在控制器中,我有@RequestParam,因此它没有进入控制器内部,然后我意识到客户端发送了表单数据,因此我更改为jersy的@FormDataParam并获得了MultiPart,但是其中什么也没得到。
@FormDataParam(PESConstants.PARAM_PDF)InputStream inputStream
我没有收到任何错误,但是:InputStream对象中为空