Search in sources :

Example 21 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project xmall by Exrick.

the class ImageController method kindeditor.

@RequestMapping(value = "/kindeditor/imageUpload", method = RequestMethod.POST)
@ApiOperation(value = "KindEditor图片上传")
public KindEditorResult kindeditor(@RequestParam("imgFile") MultipartFile files, HttpServletRequest request) {
    KindEditorResult kindEditorResult = new KindEditorResult();
    // 文件保存路径
    String filePath = request.getSession().getServletContext().getRealPath("/upload") + "\\" + QiniuUtil.renamePic(files.getOriginalFilename());
    // 检查文件
    String message = QiniuUtil.isValidImage(request, files);
    if (!message.equals("valid")) {
        kindEditorResult.setError(1);
        kindEditorResult.setMessage(message);
        return kindEditorResult;
    }
    // 转存文件
    try {
        // 保存至服务器
        File file = new File((filePath));
        files.transferTo(file);
        // 上传七牛云服务器
        String imagePath = QiniuUtil.qiniuUpload(filePath);
        if (imagePath.contains("error")) {
            kindEditorResult.setError(1);
            kindEditorResult.setMessage("上传失败");
            return kindEditorResult;
        }
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
        }
        kindEditorResult.setError(0);
        kindEditorResult.setUrl(imagePath);
        return kindEditorResult;
    } catch (IOException e) {
        e.printStackTrace();
    }
    kindEditorResult.setError(1);
    kindEditorResult.setMessage("上传失败");
    return kindEditorResult;
}
Also used : KindEditorResult(cn.exrick.common.pojo.KindEditorResult) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 22 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project xmall by Exrick.

the class ImageController method uploadFile.

@RequestMapping(value = "/image/imageUpload", method = RequestMethod.POST)
@ApiOperation(value = "WebUploader图片上传")
public Result<Object> uploadFile(@RequestParam("file") MultipartFile files, HttpServletRequest request) {
    String imagePath = null;
    // 文件保存路径
    String filePath = request.getSession().getServletContext().getRealPath("/upload") + "\\" + QiniuUtil.renamePic(files.getOriginalFilename());
    // 转存文件
    try {
        // 保存至服务器
        File file = new File((filePath));
        files.transferTo(file);
        // 上传七牛云服务器
        imagePath = QiniuUtil.qiniuUpload(filePath);
        if (imagePath.contains("error")) {
            return new ResultUtil<Object>().setErrorMsg("上传失败");
        }
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new ResultUtil<Object>().setData(imagePath);
}
Also used : File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project paascloud-master by paascloud.

the class OptAttachmentServiceImpl method uploadFile.

@Override
public List<OptUploadFileRespDto> uploadFile(MultipartHttpServletRequest multipartRequest, OptUploadFileReqDto optUploadFileReqDto, LoginAuthDto loginAuthDto, boolean storeDbFlag) {
    String fileType = optUploadFileReqDto.getFileType();
    String filePath = optUploadFileReqDto.getFilePath();
    String bucketName = optUploadFileReqDto.getBucketName();
    List<OptUploadFileRespDto> result = Lists.newArrayList();
    if (PublicUtil.isEmpty(filePath)) {
        filePath = GlobalConstant.Sys.DEFAULT_FILE_PATH;
    }
    try {
        List<MultipartFile> fileList = multipartRequest.getFiles("file");
        if (fileList.isEmpty()) {
            return result;
        }
        for (MultipartFile multipartFile : fileList) {
            String fileName = multipartFile.getOriginalFilename();
            if (PublicUtil.isEmpty(fileName)) {
                continue;
            }
            Preconditions.checkArgument(multipartFile.getSize() <= GlobalConstant.FILE_MAX_SIZE, "上传文件不能大于5M");
            InputStream inputStream = multipartFile.getInputStream();
            String inputStreamFileType = FileTypeUtil.getType(inputStream);
            CheckFileUtil.checkFileType(fileType, inputStreamFileType);
            OptUploadFileRespDto fileInfo;
            if (storeDbFlag) {
                fileInfo = this.uploadFile(multipartFile.getBytes(), fileName, fileType, filePath, bucketName, loginAuthDto);
            } else {
                fileInfo = optOssService.uploadFile(multipartFile.getBytes(), fileName, filePath, bucketName);
            }
            result.add(fileInfo);
        }
    } catch (IOException e) {
        logger.error("上传文件失败={}", e.getMessage(), e);
    }
    return result;
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 24 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project gocd by gocd.

the class ArtifactsController method updateChecksumFile.

private boolean updateChecksumFile(MultipartHttpServletRequest request, JobIdentifier jobIdentifier, String filePath) throws IOException, IllegalArtifactLocationException {
    MultipartFile checksumMultipartFile = getChecksumFile(request);
    if (checksumMultipartFile != null) {
        String checksumFilePath = String.format("%s/%s/%s", artifactsService.findArtifactRoot(jobIdentifier), ArtifactLogUtil.CRUISE_OUTPUT_FOLDER, ArtifactLogUtil.MD5_CHECKSUM_FILENAME);
        File checksumFile = artifactsService.getArtifactLocation(checksumFilePath);
        synchronized (checksumFilePath.intern()) {
            return artifactsService.saveOrAppendFile(checksumFile, checksumMultipartFile.getInputStream());
        }
    } else {
        LOGGER.warn("[Artifacts Upload] Checksum file not uploaded for artifact at path '{}'", filePath);
    }
    return true;
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 25 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project gocd by gocd.

the class ArtifactsController method postArtifact.

@RequestMapping(value = "/repository/restful/artifact/POST/*", method = RequestMethod.POST)
public ModelAndView postArtifact(@RequestParam("pipelineName") String pipelineName, @RequestParam("pipelineLabel") String counterOrLabel, @RequestParam("stageName") String stageName, @RequestParam(value = "stageCounter", required = false) String stageCounter, @RequestParam("buildName") String buildName, @RequestParam(value = "buildId", required = false) Long buildId, @RequestParam("filePath") String filePath, @RequestParam(value = "attempt", required = false) Integer attempt, MultipartHttpServletRequest request) throws Exception {
    JobIdentifier jobIdentifier;
    if (!headerConstraint.isSatisfied(request)) {
        return ResponseCodeView.create(HttpServletResponse.SC_BAD_REQUEST, "Missing required header 'Confirm'");
    }
    try {
        jobIdentifier = restfulService.findJob(pipelineName, counterOrLabel, stageName, stageCounter, buildName, buildId);
    } catch (Exception e) {
        return buildNotFound(pipelineName, counterOrLabel, stageName, stageCounter, buildName);
    }
    int convertedAttempt = attempt == null ? 1 : attempt;
    try {
        File artifact = artifactsService.findArtifact(jobIdentifier, filePath);
        if (artifact.exists() && artifact.isFile()) {
            return FileModelAndView.fileAlreadyExists(filePath);
        }
        MultipartFile multipartFile = multipartFile(request);
        if (multipartFile == null) {
            return FileModelAndView.invalidUploadRequest();
        }
        boolean success = saveFile(convertedAttempt, artifact, multipartFile, shouldUnzipStream(multipartFile));
        if (!success) {
            return FileModelAndView.errorSavingFile(filePath);
        }
        success = updateChecksumFile(request, jobIdentifier, filePath);
        if (!success) {
            return FileModelAndView.errorSavingChecksumFile(filePath);
        }
        return FileModelAndView.fileCreated(filePath);
    } catch (IllegalArtifactLocationException e) {
        return FileModelAndView.forbiddenUrl(filePath);
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) IllegalArtifactLocationException(com.thoughtworks.go.domain.exception.IllegalArtifactLocationException) JobIdentifier(com.thoughtworks.go.domain.JobIdentifier) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) IllegalArtifactLocationException(com.thoughtworks.go.domain.exception.IllegalArtifactLocationException) HeaderConstraint(com.thoughtworks.go.server.security.HeaderConstraint) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)76 File (java.io.File)26 IOException (java.io.IOException)23 Test (org.junit.Test)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)14 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)12 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)11 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 MethodParameter (org.springframework.core.MethodParameter)8 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)7 ApiOperation (io.swagger.annotations.ApiOperation)6 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)6 RequestParam (org.springframework.web.bind.annotation.RequestParam)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileOutputStream (java.io.FileOutputStream)5 LinkedList (java.util.LinkedList)5 ActivitiException (org.activiti.engine.ActivitiException)5