Search in sources :

Example 51 with MultipartFile

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);
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ImmutableList(com.google.common.collect.ImmutableList)

Example 52 with MultipartFile

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());
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 53 with MultipartFile

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;
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ArrayList(java.util.ArrayList)

Example 54 with MultipartFile

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;
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 55 with MultipartFile

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);
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ApiRest(build.dream.common.api.ApiRest) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)272 File (java.io.File)151 IOException (java.io.IOException)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)71 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)53 Test (org.junit.Test)41 FileInputStream (java.io.FileInputStream)37 ArrayList (java.util.ArrayList)30 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)28 Test (org.junit.jupiter.api.Test)27 InputStream (java.io.InputStream)25 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)24 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 FileOutputStream (java.io.FileOutputStream)18 Date (java.util.Date)18 List (java.util.List)18 PostMapping (org.springframework.web.bind.annotation.PostMapping)17 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)17 Path (java.nio.file.Path)15