¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.common.utils.file; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLEncoder; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | /** |
| | | * æä»¶å¤çå·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileUtils |
| | | { |
| | | public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+"; |
| | | |
| | | /** |
| | | * è¾åºæå®æä»¶çbyteæ°ç» |
| | | * |
| | | * @param filePath æä»¶è·¯å¾ |
| | | * @param os è¾åºæµ |
| | | * @return |
| | | */ |
| | | public static void writeBytes(String filePath, OutputStream os) throws IOException |
| | | { |
| | | FileInputStream fis = null; |
| | | try |
| | | { |
| | | File file = new File(filePath); |
| | | if (!file.exists()) |
| | | { |
| | | throw new FileNotFoundException(filePath); |
| | | } |
| | | fis = new FileInputStream(file); |
| | | byte[] b = new byte[1024]; |
| | | int length; |
| | | while ((length = fis.read(b)) > 0) |
| | | { |
| | | os.write(b, 0, length); |
| | | } |
| | | } |
| | | catch (IOException e) |
| | | { |
| | | throw e; |
| | | } |
| | | finally |
| | | { |
| | | if (os != null) |
| | | { |
| | | try |
| | | { |
| | | os.close(); |
| | | } |
| | | catch (IOException e1) |
| | | { |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | if (fis != null) |
| | | { |
| | | try |
| | | { |
| | | fis.close(); |
| | | } |
| | | catch (IOException e1) |
| | | { |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å 餿件 |
| | | * |
| | | * @param filePath æä»¶ |
| | | * @return |
| | | */ |
| | | public static boolean deleteFile(String filePath) |
| | | { |
| | | boolean flag = false; |
| | | File file = new File(filePath); |
| | | // è·¯å¾ä¸ºæä»¶ä¸ä¸ä¸ºç©ºåè¿è¡å é¤ |
| | | if (file.isFile() && file.exists()) |
| | | { |
| | | file.delete(); |
| | | flag = true; |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | | * æä»¶åç§°éªè¯ |
| | | * |
| | | * @param filename æä»¶åç§° |
| | | * @return true æ£å¸¸ false éæ³ |
| | | */ |
| | | public static boolean isValidFilename(String filename) |
| | | { |
| | | return filename.matches(FILENAME_PATTERN); |
| | | } |
| | | |
| | | /** |
| | | * ä¸è½½æä»¶åéæ°ç¼ç |
| | | * |
| | | * @param request 请æ±å¯¹è±¡ |
| | | * @param fileName æä»¶å |
| | | * @return ç¼ç åçæä»¶å |
| | | */ |
| | | public static String setFileDownloadHeader(HttpServletRequest request, String fileName) |
| | | throws UnsupportedEncodingException |
| | | { |
| | | final String agent = request.getHeader("USER-AGENT"); |
| | | String filename = fileName; |
| | | if (agent.contains("MSIE")) |
| | | { |
| | | // IEæµè§å¨ |
| | | filename = URLEncoder.encode(filename, "utf-8"); |
| | | filename = filename.replace("+", " "); |
| | | } |
| | | else if (agent.contains("Firefox")) |
| | | { |
| | | // ç«çæµè§å¨ |
| | | filename = new String(fileName.getBytes(), "ISO8859-1"); |
| | | } |
| | | else if (agent.contains("Chrome")) |
| | | { |
| | | // googleæµè§å¨ |
| | | filename = URLEncoder.encode(filename, "utf-8"); |
| | | } |
| | | else |
| | | { |
| | | // å
¶å®æµè§å¨ |
| | | filename = URLEncoder.encode(filename, "utf-8"); |
| | | } |
| | | return filename; |
| | | } |
| | | } |