package com.fh.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.io.FileUtils; import org.springframework.web.multipart.MultipartFile; /** * 說明:上傳檔案 * 作者:FH Admin * 官網:fhadmin.cn */ public class FileUpload { /**上傳檔案 * @param file //檔案物件 * @param filePath //上傳路徑 * @param fileName //檔案名 * @return 檔案名 */ public static String fileUp(MultipartFile file, String filePath, String fileName){ String extName = ""; // 擴展名格式: try { if (file.getOriginalFilename().lastIndexOf(".") >= 0){ extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); } copyFile(file.getInputStream(), filePath, fileName+extName).replaceAll("-", ""); } catch (IOException e) { System.out.println(e); } return fileName+extName; } /** * 寫檔案到當前目錄的upload目錄中 * @param in * @param fileName * @throws IOException */ public static String copyFile(InputStream in, String dir, String realName) throws IOException { File file = mkdirsmy(dir,realName); FileUtils.copyInputStreamToFile(in, file); in.close(); return realName; } /**判斷路徑是否存在,否:創建此路徑 * @param dir 檔案路徑 * @param realName 檔案名 * @throws IOException */ public static File mkdirsmy(String dir, String realName) throws IOException{ File file = new File(dir, realName); if (!file.exists()) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } return file; } /**下載網路圖片上傳到服務器上 * @param httpUrl 圖片網路地址 * @param filePath 圖片保存路徑 * @param myFileName 圖片檔案名(null時用網路圖片原名) * @return 回傳圖片名稱 */ public static String getHtmlPicture(String httpUrl, String filePath , String myFileName) { URL url; //定義URL物件url BufferedInputStream in; //定義輸入位元組緩沖流物件in FileOutputStream file; //定義檔案輸出流物件file try { String fileName = null == myFileName?httpUrl.substring(httpUrl.lastIndexOf("/")).replace("/", ""):myFileName; //圖片檔案名(null時用網路圖片原名) url = new URL(httpUrl); //初始化url物件 in = new BufferedInputStream(url.openStream()); //初始化in物件,也就是獲得url位元組流 //file = new FileOutputStream(new File(filePath +"\\"+ fileName)); file = new FileOutputStream(mkdirsmy(filePath,fileName)); int t; while ((t = in.read()) != -1) { file.write(t); } file.close(); in.close(); return fileName; } catch (MalformedURLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } } package com.fh.util; import java.io.BufferedOutputStream; import java.io.OutputStream; import java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; /** * 說明:下載檔案 * 作者:FH Admin * 官網:fhadmin.cn */ public class FileDownload { /** * @param response * @param filePath //檔案完整路徑(包括檔案名和擴展名) * @param fileName //下載后看到的檔案名 * @return 檔案名 */ public static void fileDownload(final HttpServletResponse response, String filePath, String fileName) throws Exception{ byte[] data =https://www.cnblogs.com/m17054598469/p/ FileUtil.toByteArray2(filePath); fileName = URLEncoder.encode(fileName, "UTF-8"); response.reset(); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); response.addHeader("Content-Length", "" + data.length); response.setContentType("application/octet-stream;charset=UTF-8"); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); outputStream.write(data); outputStream.flush(); outputStream.close(); response.flushBuffer(); } }
搜索
復制
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434375.html
標籤:Java
