use of org.springframework.web.multipart.MultipartFile in project irida by phac-nml.
the class SamplesController method uploadSequenceFiles.
/**
* Upload {@link SequenceFile}'s to a sample
*
* @param sampleId
* The {@link Sample} id to upload to
* @param files
* A list of {@link MultipartFile} sequence files.
* @param response
* HTTP response object to update response status if there's an
* error.
* @throws IOException
* on upload failure
*/
@RequestMapping(value = { "/samples/{sampleId}/sequenceFiles/upload" }, method = RequestMethod.POST)
public void uploadSequenceFiles(@PathVariable Long sampleId, @RequestParam(value = "files") List<MultipartFile> files, HttpServletResponse response) throws IOException {
Sample sample = sampleService.read(sampleId);
final Map<String, List<MultipartFile>> pairedFiles = SamplePairer.getPairedFiles(files);
final List<MultipartFile> singleFiles = SamplePairer.getSingleFiles(files);
for (String key : pairedFiles.keySet()) {
List<MultipartFile> list = pairedFiles.get(key);
createSequenceFilePairsInSample(list, sample);
}
for (MultipartFile file : singleFiles) {
createSequenceFileInSample(file, sample);
}
}
use of org.springframework.web.multipart.MultipartFile in project irida by phac-nml.
the class SamplesControllerTest method testUploadSequenceFiles.
// ************************************************************************************************
// AJAX REQUESTS
// ************************************************************************************************
@Test
public void testUploadSequenceFiles() throws IOException {
Sample sample = TestDataFactory.constructSample();
when(sampleService.read(sample.getId())).thenReturn(sample);
List<MultipartFile> fileList = createMultipartFileList(MULTIPARTFILE_PATHS);
ArgumentCaptor<SingleEndSequenceFile> sequenceFileArgumentCaptor = ArgumentCaptor.forClass(SingleEndSequenceFile.class);
HttpServletResponse response = new MockHttpServletResponse();
controller.uploadSequenceFiles(sample.getId(), fileList, response);
assertEquals("Response is ok", HttpServletResponse.SC_OK, response.getStatus());
verify(sequencingObjectService, times(2)).createSequencingObjectInSample(sequenceFileArgumentCaptor.capture(), eq(sample));
assertEquals("Should have the correct file name", "test_file_B.fastq", sequenceFileArgumentCaptor.getValue().getLabel());
}
use of org.springframework.web.multipart.MultipartFile in project irida by phac-nml.
the class SamplesControllerTest method createMultipartFileList.
/**
* Create a list of {@link MultipartFile}
*
* @param list
* A list of paths to files.
* @return
* @throws IOException
*/
private List<MultipartFile> createMultipartFileList(String[] list) throws IOException {
List<MultipartFile> fileList = new ArrayList<>();
for (String pathName : list) {
Path path = Paths.get(pathName);
byte[] bytes = Files.readAllBytes(path);
fileList.add(new MockMultipartFile(path.getFileName().toString(), path.getFileName().toString(), "octet-stream", bytes));
}
return fileList;
}
use of org.springframework.web.multipart.MultipartFile in project classify-system by anverliedoit.
the class XcellHelperBean method convert.
public File convert(MultipartFile file) {
try {
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos;
fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of org.springframework.web.multipart.MultipartFile in project erp-catering by liuyandong33.
the class PosController method uploadFile.
@RequestMapping(value = "/uploadFile")
@ResponseBody
public String uploadFile(HttpServletRequest httpServletRequest) {
ApiRest apiRest = null;
Map<String, String> requestParameters = ApplicationHandler.getRequestParameters();
try {
Validate.isTrue(httpServletRequest instanceof MultipartHttpServletRequest, "请上传文件!");
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) httpServletRequest;
MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
Validate.notNull(multipartFile, "请上传文件!");
String tenantId = requestParameters.get("tenantId");
ApplicationHandler.notBlank(tenantId, "tenantId");
String branchId = requestParameters.get("branchId");
ApplicationHandler.notBlank(branchId, "branchId");
String userId = requestParameters.get("userId");
ApplicationHandler.notBlank(userId, "userId");
String type = requestParameters.get("type");
ApplicationHandler.notBlank(type, "type");
String originalFilename = multipartFile.getOriginalFilename();
String posDataPath = ConfigurationUtils.getConfiguration(Constants.POS_DATA_PATH);
String directoryPath = null;
if ("database".equals(type)) {
directoryPath = posDataPath + File.separator + tenantId + File.separator + branchId + File.separator + userId + File.separator + "databases";
} else if ("log".equals(type)) {
directoryPath = posDataPath + File.separator + tenantId + File.separator + branchId + File.separator + userId + File.separator + "logs";
}
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
File file = new File(directoryPath + File.separator + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + originalFilename);
multipartFile.transferTo(file);
apiRest = new ApiRest();
apiRest.setMessage("上传文件成功!");
} catch (Exception e) {
LogUtils.error("上传文件失败", controllerSimpleName, "uploadFile", e, requestParameters);
apiRest = new ApiRest(e);
}
return GsonUtils.toJson(apiRest);
}
Aggregations