File upload (save locally) and convert files to base64

//File upload related code

 @RequestMapping(value = "/testUploadFileTwo")

 @ResponseBody

 public String upload( HttpServletRequest Request,@RequestParam MultipartFile file1) {

   if (file1.isEmpty()) {

     return "The file is empty";

   }

   // Allowed file types to be uploaded

//    String fileType = "mp3,mp4,video,rmvb,pdf,txt,xml,doc,gif,png,bmp,jpeg";

   //The maximum size of the file allowed to be uploaded (100M, the unit is byte)

//    int maxSize = 1024*1024*100;

   // get filename

   String fileName = file1.getOriginalFilename();

   //Get the upload file size

   String size = FormetFileSize(file1.getSize());

   // Get the file extension

//    String suffixName = fileName.substring(fileName.lastIndexOf("."));

   // path after file upload

   String filePath = "E://testNew//";

   // Convert base64String to path to file

   String fileNewPath = "E://testNew64//"+fileName;

   // Solve the Chinese problem, the Chinese path under liunx, the picture display problem

//    fileName = UUID.randomUUID() + suffixName;

   filePath = filePath + fileName;

   File dest = new File(filePath);

   // Check if the directory exists

   if (!dest.getParentFile().exists()) 

   {

     dest.getParentFile (). mkdirs ();

   }

   try 

   {

   file1.transferTo (dest);

   // Convert the file of the specified path to a base64 string

   String base64String = MainController.file2Str(filePath);

   // Convert the specified base64 string file to a file

   MainController.str2File(base64String, fileNewPath);

   return "Upload successful";

   } catch (IllegalStateException e) {

     e.printStackTrace ();

   } catch (IOException e) {

     e.printStackTrace ();

   }

   return "Upload failed";

 }

 

 /**

  * Convert the file to a byte array string and Base64 encode it  

  * @param file

  * @return

  */

   public static String file2Str(String file){  

       InputStream in = null;  

       byte[] data = null;  

       //read file byte array  

       try{  

           in = new FileInputStream(file);          

           data = new byte[in.available()];  

           in.read(data);  

           in.close();  

       } catch (IOException e) {  

           e.printStackTrace ();  

       }  

       return new String(Base64.encodeBase64(data));  

   }  

   

   /**

    * Base64 decoding  

    * @param fileStr the string to be transcoded

    * @param filePath generated new path

    * @return

    */

   public static boolean str2File(String fileStr,String filePath){  

       if (fileStr == null) //The file data is empty  

           return false;  

       try {  

           byte[] b = Base64.decodeBase64(fileStr);  

           for(int i=0;i<b.length;++i){  

               if(b[i]<0){//Adjust abnormal data  

                   b[i]+=256;   

               }  

           }  

           // Check if there is a directory, no new file exists

           File dest = new File(filePath);

       if (!dest.getParentFile().exists()) {

         dest.getParentFile (). mkdirs ();

       }

           // Generate a file and save it in a file on the server's hard disk 

           OutputStream out = new FileOutputStream(dest);      

           out.write(b);  

           out.flush();  

           out.close();  

           return true;  

       } catch (Exception e)  {  

           e.printStackTrace ();  

           return false;  

       }  

   } 

   

   /**

    * Convert file size

    * @param fileS

    * @return

    */

   public String FormetFileSize(long fileS) {

       DecimalFormat df = new DecimalFormat("#.00");

       String fileSizeString = "";

       if (fileS < 1024) {

           fileSizeString = df.format((double) fileS) + "B";

       } else if (fileS < 1048576) {

           fileSizeString = df.format((double) fileS / 1024) + "K";

       } else if (fileS < 1073741824) {

           fileSizeString = df.format((double) fileS / 1048576) + "M";

       } else {

           fileSizeString = df.format((double) fileS / 1073741824) +"G";

       }

       return fileSizeString;

    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394704&siteId=291194637