【发布时间】:2019-07-26 06:21:58
【问题描述】:
我正在从事从 j2ee 到 Springboot 的迁移项目。我确实很难在 Spring Boot 中上传文件。正如在J2EE应用程序中使用“org.apache.tomcat.util.http.fileupload”来上传文件一样,我认为spring-boot已经嵌入了tomcat,它可以工作,但不幸的是它没有。我必须与 Multipart 一起工作。我不能在任何时候让它为假。我用 apache common 尝试过的同样的事情最终得到了相同的结果。这是我的代码..您能否建议我如何进行..您的帮助将不胜感激..
我也有两个过滤器。我使用邮递员进行测试
import org.apache.tomcat.util.http.fileupload.FileItemIterator;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
@RestController
@RequestMapping("/v2")
public class PDFExtractController {
private static final long serialVersionUID = 1L;
private static Logger log = Logger.getLogger(PDFExtractController.class);
// @RequestHeader(PESConstants.CLIENT_CONTEXT) String intuit_clientcontext, // deleted from the function parameter
@RequestMapping(value = "document/{DocType}",method =RequestMethod.POST,consumes = "multipart/form-data")
public ResponseData fileUpload(@PathVariable("DocType") String docType, HttpServletRequest request){
boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
String provider = null;
String password = null;
boolean verbose = false;
if (isMultipartContent) {
// Grab the content
try {
ServletFileUpload fileUpload = new ServletFileUpload();
FileItemIterator items = fileUpload.getItemIterator(request);
while (items.hasNext()) {
FileItemStream item = items.next();
String fieldname = item.getFieldName();
if (PESConstants.PARAM_PROVIDER.equals(fieldname)) {
provider = PdfServletMgr.getTextValue(item);
SplunkMgr.addtoMDC(MDCFieldNames.PROVIDER.getValue(), provider.trim());
}
else if (PESConstants.PARAM_VERBOSE.equals(fieldname)) {
verbose = "true".equals(PdfServletMgr.getTextValue(item));
}
if (PESConstants.PARAM_PDF.equals(fieldname) && "form".equalsIgnoreCase(docType)) {
file = PdfServletMgr.getValue(item);
}
else if (PESConstants.PARAM_PASSWORD.equals(fieldname)) {
password = PdfServletMgr.getTextValue(item);
}
}
}
}
}
}
【问题讨论】:
-
我已经解决了这个问题...得到了答案部分中的解决方案..我的建议是永远不要使用 apache common 或 apache http fileupload,如果您使用的是 spring-boot,请使用 springbot Multipartfile
标签: apache spring-boot migration multifile-uploader