[Tools] java upload files

    / ** upload files 
     * @param File file 
     * @param filePath upload file path does not include the file name 
     * @param fileName new file name 
     * @return return to a path name 
     * @throws Exception
      * / 
    public  static String uploadFile (File MultipartFile , String filePath, String fileName) throws Exception {
         // original filename 
        String filename = file.getOriginalFilename ();
         // get the file extension 
        String suffix = filename.substring (filename.lastIndexOf ( "." ));
         //Determining whether the directory is empty, when a new empty directory 
        File targetfile = new new File (filePath);
         IF (! {TargetFile.exists ()) 
            targetFile.mkdirs (); 
        } 

        // upload file path 
        String path = filePath + "/" + + fileName suffix;
         // upload 
        a FileOutputStream OUT = new new a FileOutputStream (path); 
        out.write (file.getBytes ()); 
        out.flush (); 
        the out.close (); 
        return path; 
    }

transfer:

    @ApiOperation("上传文件")
    @PostMapping(value = "/uploadFile", consumes = "multipart/*",headers = "content-type=multipart/form-data")
    @ResponseBody
    public void uploadFile(
            @RequestParam MultipartFile file)
            throws Exception {
        FileUpload.uploadFile(file,"G:/pic","123");
        try {

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

 

Guess you like

Origin www.cnblogs.com/tuituji27/p/11346244.html