use of org.springframework.web.multipart.MultipartHttpServletRequest in project plumdo-work by wengwh.
the class ProcessDefinitionImportResource method createProcessDefinition.
@PostMapping(value = "/process-definitions/import", name = "流程定义导入")
@ResponseStatus(value = HttpStatus.CREATED)
public ProcessDefinitionResponse createProcessDefinition(@RequestParam(value = "tenantId", required = false) String tenantId, HttpServletRequest request) {
if (!(request instanceof MultipartHttpServletRequest)) {
exceptionFactory.throwIllegalArgument(ErrorConstant.REQUEST_NOT_MULTIPART);
}
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
if (multipartRequest.getFileMap().size() == 0) {
exceptionFactory.throwIllegalArgument(ErrorConstant.MULTIPART_CONTENT_EMPTY);
}
MultipartFile file = multipartRequest.getFileMap().values().iterator().next();
String fileName = file.getOriginalFilename();
if (ObjectUtils.isEmpty(fileName) || !(fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn") || fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip"))) {
exceptionFactory.throwIllegalArgument(ErrorConstant.FILE_NOT_BPMN, fileName);
}
try {
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
if (fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn")) {
deploymentBuilder.addInputStream(fileName, file.getInputStream());
} else if (fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip")) {
deploymentBuilder.addZipInputStream(new ZipInputStream(file.getInputStream()));
}
deploymentBuilder.name(fileName);
if (tenantId != null) {
deploymentBuilder.tenantId(tenantId);
}
String deploymentId = deploymentBuilder.deploy().getId();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
return restResponseFactory.createProcessDefinitionResponse(processDefinition);
} catch (Exception e) {
logger.error("导入流程文件异常", e);
exceptionFactory.throwDefinedException(ErrorConstant.DEFINITION_IMPORT_FILE_ERROR, fileName, e.getMessage());
return null;
}
}
use of org.springframework.web.multipart.MultipartHttpServletRequest in project ma-core-public by infiniteautomation.
the class FileUploadController method prepareResponse.
/**
* @param request
* @param response
* @param model
* @throws IOException
* @throws JsonException
* @throws FileUploadException
*/
private FileUploadView prepareResponse(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) throws IOException, JsonException, FileUploadException {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
String dataType = multipartRequest.getParameter("dataType");
String uploadType = multipartRequest.getParameter("uploadType");
List<Object> fileInfo = new ArrayList<Object>();
Translations translations = ControllerUtils.getTranslations(request);
Iterator<String> itr = multipartRequest.getFileNames();
while (itr.hasNext()) {
MultipartFile file = multipartRequest.getFile(itr.next());
if (!file.isEmpty()) {
Map<String, Object> info = new HashMap<String, Object>();
info.put("filename", file.getOriginalFilename());
info.put("dataType", dataType);
parseFile(file.getInputStream(), info, translations, request);
fileInfo.add(info);
}
}
model.put("fileInfo", fileInfo);
boolean iframe = false;
boolean html5 = false;
boolean flash = false;
if (uploadType.equals("iframe")) {
iframe = true;
response.setContentType("text/html");
} else if (uploadType.equals("html5")) {
html5 = true;
response.setContentType("application/json");
} else if (uploadType.equals("flash")) {
flash = true;
response.setContentType("text/plain");
}
if (iframe || html5) {
return new FileUploadView(iframe);
} else if (flash) {
// TODO handle Flash
throw new ShouldNeverHappenException("Flash upload not supported.");
} else {
throw new ShouldNeverHappenException("Invalid file upload type.");
}
}
use of org.springframework.web.multipart.MultipartHttpServletRequest in project spring-framework by spring-projects.
the class DispatcherServletTests method existingMultipartRequest.
@Test
public void existingMultipartRequest() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
request.addPreferredLocale(Locale.CANADA);
request.addUserRole("role1");
MockHttpServletResponse response = new MockHttpServletResponse();
ComplexWebApplicationContext.MockMultipartResolver multipartResolver = (ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext().getBean("multipartResolver");
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
complexDispatcherServlet.service(multipartRequest, response);
multipartResolver.cleanupMultipart(multipartRequest);
assertThat(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE)).isNull();
assertThat(request.getAttribute("cleanedUp")).isNotNull();
}
use of org.springframework.web.multipart.MultipartHttpServletRequest in project grails-core by grails.
the class DefaultUrlMappingInfo method getResolvedRequest.
private MultipartHttpServletRequest getResolvedRequest(HttpServletRequest request, MultipartResolver resolver) {
MultipartHttpServletRequest resolvedMultipartRequest = (MultipartHttpServletRequest) request.getAttribute(MultipartHttpServletRequest.class.getName());
if (resolvedMultipartRequest == null) {
resolvedMultipartRequest = resolver.resolveMultipart(request);
request.setAttribute(MultipartHttpServletRequest.class.getName(), resolvedMultipartRequest);
}
return resolvedMultipartRequest;
}
Aggregations