Search in sources :

Example 96 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project Assignment by WMPeople.

the class RestController method updateWithoutAttachment.

/**
 * 게시물에 첨부파일을 유지하고 싶은 경우를 제외하고는 글수정 데이터를 받아 DB에 등록합니다.
 * @param board 사용자가 수정한 board 데이터를 받습니다.
 * @param attachment 첨부파일 데이터를 받습니다.
 */
@RequestMapping(value = "/boards/update2", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> updateWithoutAttachment(BoardDTO board, MultipartHttpServletRequest attachment) {
    Map<String, Object> resultMap = new HashMap<>();
    MultipartFile mFile = null;
    Iterator<String> iter = attachment.getFileNames();
    while (iter.hasNext()) {
        String uploadFile_name = iter.next();
        mFile = attachment.getFile(uploadFile_name);
    }
    try {
        if (mFile != null) {
            if (!mFile.getOriginalFilename().equals("")) {
                FileDTO file = new FileDTO();
                file.setFile_name(mFile.getOriginalFilename());
                file.setFile_data(mFile.getBytes());
                file.setFile_size(mFile.getSize());
                fileMapper.createFile(file);
                board.setFile_id(file.getFile_id());
            }
        }
        NodePtrDTO leapPtrDTO = new NodePtrDTO(board.getBoard_id(), board.getVersion());
        NodePtrDTO newNode = versionManagementService.modifyVersion(board, leapPtrDTO);
        if (newNode == null) {
            resultMap.put("result", "수정 실패");
        } else {
            resultMap.put("result", "success");
        }
    } catch (Exception e) {
        resultMap.put("result", e.getMessage());
        e.printStackTrace();
        return resultMap;
    }
    return resultMap;
}
Also used : NodePtrDTO(com.worksmobile.Assignment.Domain.NodePtrDTO) MultipartFile(org.springframework.web.multipart.MultipartFile) FileDTO(com.worksmobile.Assignment.Domain.FileDTO) HashMap(java.util.HashMap) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 97 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project Assignment by WMPeople.

the class RestController method create.

/**
 * 글 생성을 합니다. VersionManagementService의 createArticle 함수를 호출하여 board_histry 테이블과 board 테이블에 데이터를 삽입합니다.
 * @param board 사용자가 작성한 board 데이터를 받습니다.
 * @param attachment 첨부파일 데이터를 받습니다.
 */
@RequestMapping(value = "/boards", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> create(BoardDTO board, MultipartHttpServletRequest attachment) {
    Map<String, Object> resultMap = new HashMap<>();
    MultipartFile mFile = null;
    Iterator<String> iter = attachment.getFileNames();
    while (iter.hasNext()) {
        String uploadFile_name = iter.next();
        mFile = attachment.getFile(uploadFile_name);
    }
    try {
        if (mFile != null) {
            if (!mFile.getOriginalFilename().equals("")) {
                FileDTO file = new FileDTO();
                file.setFile_name(mFile.getOriginalFilename());
                file.setFile_data(mFile.getBytes());
                file.setFile_size(mFile.getSize());
                fileMapper.createFile(file);
                board.setFile_id(file.getFile_id());
            } else {
                board.setFile_id(0);
            }
        }
        versionManagementService.createArticle(board);
        resultMap.put("result", "success");
    } catch (Exception e) {
        // TODO: handle exception
        resultMap.put("result", e.getMessage());
        e.printStackTrace();
    }
    return resultMap;
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) FileDTO(com.worksmobile.Assignment.Domain.FileDTO) HashMap(java.util.HashMap) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 98 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project CzechIdMng by bcvsolutions.

the class CsvConnectorTypeTest method testDeployCsv.

@Test
public void testDeployCsv() throws IOException {
    String csvName = "idm_test.csv";
    ConnectorTypeDto mockCsvConnectorTypeDto = new ConnectorTypeDto();
    mockCsvConnectorTypeDto.setReopened(false);
    mockCsvConnectorTypeDto.setId(CsvConnectorType.NAME);
    ConnectorTypeDto csvConnectorTypeDto = connectorManager.load(mockCsvConnectorTypeDto);
    assertNotNull(csvConnectorTypeDto);
    String defaultPath = csvConnectorTypeDto.getMetadata().get(CsvConnectorType.FILE_PATH);
    assertNotNull(defaultPath);
    // Convert test file to the bytes and create mock MultipartFile.
    byte[] bytes = Files.readAllBytes(Paths.get(CSV_TEST_FILE));
    MultipartFile multipartFile = new MockMultipartFile(csvName, bytes);
    // Deploy file. CSV file should be copied to the default path.
    ResponseEntity<ConnectorTypeDto> deployResponse = csvConnectorTypeController.deploy(csvName, defaultPath, multipartFile);
    ConnectorTypeDto deployResult = deployResponse.getBody();
    assertNotNull(deployResult);
    assertEquals(Paths.get(defaultPath, csvName).toString(), Paths.get(deployResult.getMetadata().get(CsvConnectorType.FILE_PATH)).toString());
    // Check if deployed file exists.
    assertTrue(Files.exists(Paths.get(defaultPath, csvName)));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) ConnectorTypeDto(eu.bcvsolutions.idm.acc.dto.ConnectorTypeDto) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 99 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")) {
            throw new XmallUploadException("上传失败");
        }
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new ResultUtil<Object>().setData(imagePath);
}
Also used : XmallUploadException(cn.exrick.common.exception.XmallUploadException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) XmallUploadException(cn.exrick.common.exception.XmallUploadException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 100 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)

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