0

Friends I Have One Problem in my Code

I want to Convert MultipartFile to pdf and Download it in Specific Location , Can any one please help me

@RequestMapping(value = "/uploadFile", method = RequestMethod.PUT, consumes = { "multipart/form-data" })
public Map<String, String> uploadFile(@RequestParam("uploadfile") MultipartFile pdfFile) throws Exception {

    String url = pdfFile + "FileName";
    File fileToSave = new File(url);
    fileToSave.createNewFile();

    FileOutputStream fos = new FileOutputStream(fileToSave);
    fos.write(pdfFile.getBytes());
    fos.close();

    return null;
}
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
AMAR MAGAR
  • 137
  • 2
  • 13

2 Answers2

0

Try this function please:

   public static File convert(MultipartFile file) throws IOException {
    File convFile = new File(file.getOriginalFilename());
    convFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(convFile);
    fos.write(file.getBytes());
    fos.close();
    return convFile;
    }
Mohamad Alhamid
  • 346
  • 4
  • 15
  • And for download file i think this help you, https://stackoverflow.com/questions/35680932/download-a-file-from-spring-boot-rest-service – Mohamad Alhamid Jul 05 '19 at 07:58
0
Thanks, guys , I got Soluation ....Following code is going to work  

.......................

@RequestMapping(value = "/uploadFile", method = RequestMethod.PUT, consumes = { "multipart/form-data" })
        public Map<String, String> uploadFile(@RequestParam("uploadfile") MultipartFile pdfFile) throws Exception {


            //Download PDF 
            try { 
            File convFile = new File(pdfFile.getOriginalFilename());
            convFile.createNewFile();
            String zipFile = "D://"+pdfFile.getOriginalFilename()+".zip";  //path


                FileOutputStream fos = new FileOutputStream(zipFile);
                ZipOutputStream zos = new ZipOutputStream(fos);
                 zos.putNextEntry(new ZipEntry(pdfFile.getOriginalFilename()));     
                  zos.write(pdfFile.getBytes());   
                   zos.closeEntry();
                   zos.close();

            }
            catch (IOException ioe) {
                System.out.println("Error creating zip file: " + ioe);
            }

    return null;
    }
AMAR MAGAR
  • 137
  • 2
  • 13