Java 將要上傳的文件上傳至指定路徑代碼實現


代碼:

/**
	 * 上傳文件到指定路徑
	 * @param mFile 要上傳的文件
	 * @param path	指定路徑
	 */
	public static void uploadFile(MultipartFile mFile, String path) {
	    try {
            InputStream in = mFile.getInputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            File file = new File(path);
            File fileParent = file.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();
            }
            OutputStream out = new FileOutputStream(path);
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.close();
            in.close();
        } catch (Exception e) {
	        System.out.println("----------" + path +"文件上傳失敗————————");
	        e.printStackTrace();
        }
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM