前幾天應聘的公司發來了這道題。。。java
需求說明:純編碼的方式實現音頻、文本文件、視頻等各類文件的上傳。要求程序的擴展性高,耦合性低。spring
何爲工廠模式:首先拋開工廠模式的概念,你們想一想生活當中工廠的做用(爲咱們生產新的商品),而在編程中工廠就是給咱們建立(new)新的對象。若是你用過spring,對工廠的理解可能會更深,由於其的設計中用到了工廠模式,和代理模式。閒言少敘,上代碼。編程
接口:緩存
1 package com.cn.DAO; 2 3 import java.io.File; 4 5 public interface IFold { 6 public boolean reader(File url); 7 }
實現類1:處理媒體類文件ide
1 package com.cn.Imp; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 9 import com.cn.DAO.IFold; 11 public class UpMadie implements IFold { 12 @Override 13 public boolean reader(File url) { 14 15 FileInputStream in = null; 16 FileOutputStream out = null; 17 String outUrl = "src/getAllFiles/" + url.getName().toString(); 18 File upFiles = new File("src/getAllFiles"); 19 // 判斷要上傳的目錄下是否存在同名文件,若是存在,先刪除,再上傳 20 for (File file : upFiles.listFiles()) { 21 if (file.getName().equals(url.getName())) { 22 file.delete(); 23 System.out.println("重複文件已近刪除,開始上傳【媒體文件】"); 24 break; 25 } 26 } 28 try { 29 in = new FileInputStream(url); 30 out = new FileOutputStream(new File(outUrl)); 31 byte[] b = new byte[1024]; 32 33 int lengs; 34 while ((lengs = in.read(b)) != -1) { 35 out.write(b, 0, lengs); 36 } 38 return true; 39 } catch (FileNotFoundException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } catch (IOException e) { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 }finally{ 46 47 try { 48 in.close(); 49 out.close(); 50 } catch (IOException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 57 } 58 return false; 59 } 60 61 }
實現類2:處理文本文件,引入了緩存工具
1 package com.cn.Imp; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStream; 11 import java.io.InputStreamReader; 12 import java.io.OutputStream; 13 import java.io.OutputStreamWriter; 14 15 import com.cn.DAO.IFold; 16 17 /** 18 * 字符流 讀取文本:txt,doc,pdf 19 */ 20 public class UpWords implements IFold { 21 22 @Override 23 public boolean reader(File url) { 24 InputStream in = null; 25 InputStreamReader ireader = null; 26 BufferedReader breader = null; 27 OutputStream out = null; 28 OutputStreamWriter owriter = null; 29 BufferedWriter bWriter = null; 30 String outUrl = "src/getAllFiles/" + url.getName().toString(); 31 File upFiles = new File("src/getAllFiles"); 32 // 判斷要上傳的目錄下是否存在同名文件,若是存在,先刪除,再上傳 33 for (File file : upFiles.listFiles()) { 34 if (file.getName().equals(url.getName())) { 35 file.delete(); 36 System.out.println("重複文件已近刪除,開始上傳【文本文件】"); 37 break; 38 } 39 } 40 41 try { 42 in = new FileInputStream(url); 43 ireader = new InputStreamReader(in); 44 breader = new BufferedReader(ireader); 45 out = new FileOutputStream(new File("src/getAllFiles/" 46 + url.getName())); 47 owriter = new OutputStreamWriter(out); 48 bWriter = new BufferedWriter(owriter); 49 50 String line; 51 while ((line = breader.readLine()) != null) { 52 bWriter.write(line); 53 } 54 return true; 55 } catch (FileNotFoundException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } catch (IOException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } finally { 62 try { 63 bWriter.flush(); 64 in.close(); 65 ireader.close(); 66 breader.close(); 67 out.close(); 68 owriter.close(); 69 bWriter.close(); 70 71 } catch (IOException e) { 72 // TODO Auto-generated catch block 73 e.printStackTrace(); 74 } 75 } 76 return false; 77 } 78 79 }
工具類1:讀屬性文件測試
1 package com.cn.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Enumeration; 6 import java.util.HashMap; 7 import java.util.Map; 8 import java.util.Properties; 9 10 public class PropertiesReader { 11 12 public Map<String, String> getProperties(){ 13 Properties prop = new Properties(); 14 Map<String, String> map = new HashMap<String, String>(); 15 try { 16 InputStream in = getClass().getResourceAsStream("file.properties"); 17 prop.load(in); 18 Enumeration en = prop.propertyNames(); 19 while (en.hasMoreElements()) { 20 String key = (String)en.nextElement(); 21 String property = prop.getProperty(key); 22 map.put(key, property); 23 } 24 } catch (IOException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } 28 return map; 29 } 30 }
工具類2:彈框選擇上傳的文件編碼
package com.cn.util; import java.io.File; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.filechooser.FileSystemView; public class GetFile { public File backFile() { JFileChooser jfc = new JFileChooser(); // 設置當前路徑爲桌面路徑,不然將個人文檔做爲默認路徑 FileSystemView fsv = FileSystemView.getFileSystemView(); jfc.setCurrentDirectory(fsv.getHomeDirectory()); // JFileChooser.FILES_AND_DIRECTORIES 選擇路徑和文件 jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 彈出的提示框的標題 jfc.showDialog(new JLabel(), "肯定"); // 用戶選擇的路徑或文件 File file = jfc.getSelectedFile(); return file; } }
屬性文件:file.propertiesurl
1 jpg=com.cn.Imp.UpMadie 2 png=com.cn.Imp.UpMadie 3 mp4=com.cn.Imp.UpMadie 4 mp3=com.cn.Imp.UpMadie 5 wma=com.cn.Imp.UpMadie 6 pdf=com.cn.Imp.UpWords 7 doc=com.cn.Imp.UpWords 8 txt=com.cn.Imp.UpWords 9 docx=com.cn.Imp.UpWords
測試類:Textspa
1 package com.cn.test; 2 3 import java.io.File; 4 5 import com.cn.DAO.IFold; 6 import com.cn.factory.FoldFactory; 7 import com.cn.util.GetFile; 8 9 public class Test { 10 11 public static void main(String[] args) { 12 //獲取本地文件路徑的類 13 GetFile getback = new GetFile(); 14 File file= getback.backFile(); 15 String fileName = file.getName(); 16 String[] keys = fileName.split("\\."); 17 String key = keys[keys.length-1]; 18 //讀寫文件的工廠 19 FoldFactory factory = new FoldFactory(); 20 //接口 21 IFold iFold = factory.getReaderFold(fileName); 22 //具體實現方法 23 boolean b = iFold.reader(file); 24 if(b==true){ 25 System.out.println(key+"文件上傳成功"); 26 }else{ 27 System.out.println(key+"文件上傳失敗"); 28 } 29 30 } 31 32 }