File upload and download (b) upload files

method one:

java:

@RequestMapping("/testFile")
    public String fileUpload(MultipartFile multipartFile) {
        Path String = "d: \\ the Upload \\ \\ the Test" ;
         // formerly known as 
        String [] of the type = multipartFile.getOriginalFilename () Split ( "\\.");. // For the different image types such as: JPG 
        String filename = new new a date () the getTime () + + multipartFile.getOriginalFilename () Split ( "\\.") [-type.length. 1];.. "." // file is named by: the .xxx
         // path 
        file = file new new file (path);
         // does not exist create folders 
        IF (File.Exists () &&!! file.isDirectory ()) {
            file.mkdirs();
        }
        File myfile = new File(path+filename);
        try {
            multipartFile.transferTo(myfile);
        } catch (IllegalStateException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
        return filename;
    }

Self-test:

 

 

 The file is saved in the d disk upload test case.

 

Method Two:

public static final String FileUpload(MultipartFile multipartFile,String path) throws IOException {
            String [] of the type = multipartFile.getOriginalFilename () Split ( "\\.");. // For the different image types such as: JPG 
            String filename = new new a Date () getTime () + + multipartFile.getOriginalFilename (). "." .split ( "\\.") [type.length-1]; // this file is named: date .xxx
             // path 
            file file = new new file (path);
             // does not exist to create a file folder 
            IF (file! .exists () &&! file.isDirectory ()) {
                file.mkdirs();
            }
            OUT the DataOutputStream = new new the DataOutputStream ( new new a FileOutputStream (filename + path)); // store the absolute file path of 
            the InputStream IS = null ; // accessory input stream 
            the try {
                 IS = multipartFile.getInputStream ();
                  byte [] B = new new  byte [is.available ()]; // Available data stream to that number of bytes that can be read 
                 is.read (b);
                 out.write(b);
            } catch (IOException exception) {
                 exception.printStackTrace();
            } finally {
                 if (is != null) {
                  is.close();
                 }
                 if (out != null) {
                  out.close();
                 }
            }
            return filename;
    }

Guess you like

Origin www.cnblogs.com/DarGi2019/p/12122700.html