Search in sources :

Example 61 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project ngtesting-platform by aaronchen2k.

the class FileUtils method SaveFile.

public static String SaveFile(MultipartFile file, String uploadRelativeDist, String fileName) {
    String dateDist = DateUtils.GetDateNoSeparator();
    String uploadPath = Constant.FTP_UPLOAD_DIR + uploadRelativeDist + dateDist + "/";
    String localFolder = Constant.WORK_DIR + uploadPath;
    FileUtils.CreateDirIfNeeded(localFolder);
    String localPath = localFolder + fileName;
    File localFile = new File(localPath);
    try {
        file.transferTo(localFile);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    FileUtils.Thumb(localPath);
    return uploadPath + fileName;
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 62 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-boot-quick by vector4wang.

the class Img2TxtController method imt2txt.

@RequestMapping(value = "/transfer", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<InputStreamResource> imt2txt(@RequestParam("file") MultipartFile file) {
    try {
        String originalFilename = file.getOriginalFilename();
        HttpHeaders headers = new HttpHeaders();
        // 支持jpg、png
        if (originalFilename.endsWith("jpg") || originalFilename.endsWith("png")) {
            File outFile = img2TxtService.save(file.getBytes(), originalFilename);
            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
            headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", outFile.getName()));
            headers.add("Pragma", "no-cache");
            headers.add("Expires", "0");
            return ResponseEntity.ok().headers(headers).contentLength(outFile.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new InputStreamResource(new FileInputStream(outFile)));
        } else {
            File error = new File(img2TxtService.getErrorPath());
            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
            headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", error.getName()));
            headers.add("Pragma", "no-cache");
            headers.add("Expires", "0");
            return ResponseEntity.ok().headers(headers).contentLength(error.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new InputStreamResource(new FileInputStream(error)));
        }
    } catch (IOException e) {
        logger.error(e);
        return new ResponseEntity("暂不支持的文件格式", HttpStatus.BAD_REQUEST);
    }
// return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInputStream(java.io.FileInputStream) InputStreamResource(org.springframework.core.io.InputStreamResource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 63 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project rpki-validator-3 by RIPE-NCC.

the class TrustAnchorController method add.

@PostMapping(path = "/upload", consumes = "multipart/form-data")
public ResponseEntity<ApiResponse<TrustAnchorResource>> add(@RequestParam("file") MultipartFile trustAnchorLocator, Locale locale) {
    try {
        TrustAnchorLocator locator = TrustAnchorLocator.fromMultipartFile(trustAnchorLocator);
        AddTrustAnchor command = AddTrustAnchor.builder().type(TrustAnchor.TYPE).name(locator.getCaName()).locations(locator.getCertificateLocations().stream().map(URI::toASCIIString).collect(Collectors.toList())).subjectPublicKeyInfo(locator.getPublicKeyInfo()).rsyncPrefetchUri(locator.getPrefetchUris().stream().filter(uri -> "rsync".equalsIgnoreCase(uri.getScheme())).map(URI::toASCIIString).findFirst().orElse(null)).build();
        long id = trustAnchorService.execute(command);
        TrustAnchor trustAnchor = trustAnchorRepository.get(id);
        Link selfRel = linkTo(methodOn(TrustAnchorController.class).get(id, locale)).withSelfRel();
        return ResponseEntity.created(URI.create(selfRel.getHref())).body(trustAnchorResource(trustAnchor, locale));
    } catch (TrustAnchorExtractorException ex) {
        return ResponseEntity.badRequest().body(ApiResponse.error(ApiError.of(HttpStatus.BAD_REQUEST, "Invalid trust anchor locator: " + ex.getMessage())));
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) RpkiRepository(net.ripe.rpki.validator3.domain.RpkiRepository) StringUtils(org.apache.commons.lang.StringUtils) RequestParam(org.springframework.web.bind.annotation.RequestParam) Autowired(org.springframework.beans.factory.annotation.Autowired) Valid(javax.validation.Valid) Paging(net.ripe.rpki.validator3.api.Paging) RpkiRepositories(net.ripe.rpki.validator3.domain.RpkiRepositories) Api(net.ripe.rpki.validator3.api.Api) Locale(java.util.Locale) ValidationRunController(net.ripe.rpki.validator3.api.validationruns.ValidationRunController) TrustAnchorExtractorException(net.ripe.rpki.validator3.util.TrustAnchorExtractorException) URI(java.net.URI) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) TrustAnchorLocator(net.ripe.rpki.validator3.util.TrustAnchorLocator) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) TrustAnchor(net.ripe.rpki.validator3.domain.TrustAnchor) PostMapping(org.springframework.web.bind.annotation.PostMapping) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) ValidationRun(net.ripe.rpki.validator3.domain.ValidationRun) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) ValidationRuns(net.ripe.rpki.validator3.domain.ValidationRuns) Links(org.springframework.hateoas.Links) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ControllerLinkBuilder.methodOn(org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn) Metadata(net.ripe.rpki.validator3.api.Metadata) ArrayList(java.util.ArrayList) Sorting(net.ripe.rpki.validator3.api.Sorting) RequestBody(org.springframework.web.bind.annotation.RequestBody) ControllerLinkBuilder.linkTo(org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo) GetMapping(org.springframework.web.bind.annotation.GetMapping) TrustAnchorValidationRun(net.ripe.rpki.validator3.domain.TrustAnchorValidationRun) MessageSource(org.springframework.context.MessageSource) ValidationCheckResource(net.ripe.rpki.validator3.api.validationruns.ValidationCheckResource) Link(org.springframework.hateoas.Link) ApiError(net.ripe.rpki.validator3.api.ApiError) TrustAnchors(net.ripe.rpki.validator3.domain.TrustAnchors) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpStatus(org.springframework.http.HttpStatus) ApiCommand(net.ripe.rpki.validator3.api.ApiCommand) ValidationRunResource(net.ripe.rpki.validator3.api.validationruns.ValidationRunResource) SearchTerm(net.ripe.rpki.validator3.api.SearchTerm) MultipartFile(org.springframework.web.multipart.MultipartFile) ResponseEntity(org.springframework.http.ResponseEntity) ApiResponse(net.ripe.rpki.validator3.api.ApiResponse) TrustAnchorExtractorException(net.ripe.rpki.validator3.util.TrustAnchorExtractorException) TrustAnchor(net.ripe.rpki.validator3.domain.TrustAnchor) TrustAnchorLocator(net.ripe.rpki.validator3.util.TrustAnchorLocator) URI(java.net.URI) Link(org.springframework.hateoas.Link) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 64 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project metalnx-web by irods-contrib.

the class TemplateController method importXMLFile.

@RequestMapping(value = "/import/", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public String importXMLFile(final Model model, final HttpServletRequest request, final RedirectAttributes redirect) {
    String responseString = "ok";
    if (request instanceof MultipartHttpServletRequest) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> multipartFiles = multipartRequest.getFiles("file");
        String prefix = multipartRequest.getParameter("prefix");
        String suffix = multipartRequest.getParameter("suffix");
        try {
            String username = loggedUserUtils.getLoggedDataGridUser().getUsername();
            boolean result = templateService.importXmlMetadataTemplate(multipartFiles.get(0).getInputStream(), username, prefix, suffix);
            if (!result) {
                responseString = "partial";
            }
        } catch (JAXBException | IOException | DataGridException e) {
            logger.error("Could not import metadata templates", e);
            responseString = "error";
        }
    }
    return responseString;
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) DataGridException(com.emc.metalnx.core.domain.exceptions.DataGridException) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 65 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project metalnx-web by irods-contrib.

the class TicketClientController method upload.

@RequestMapping(value = "/{ticketstring}", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void upload(@PathVariable("ticketstring") final String ticketString, final HttpServletRequest request) throws DataGridConnectionRefusedException, DataGridTicketUploadException, IOException, DataGridTicketInvalidUserException {
    logger.info("Uploading files using ticket: {}", ticketString);
    if (!(request instanceof MultipartHttpServletRequest)) {
        logger.error("Request is not a multipart request. Stop.");
        return;
    }
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    MultipartFile multipartFile = multipartRequest.getFile("file");
    String destPath = multipartRequest.getParameter("path");
    File file = multipartToFile(multipartFile);
    ticketClientService.transferFileToIRODSUsingTicket(ticketString, file, destPath);
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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