前几天应聘的公司发来了这道题。。。
需求说明:纯编码的方式实现音频、文本文件、视频等各种文件的上传。要求程序的扩展性高,耦合性低。
何为工厂模式:首先抛开工厂模式的概念,大家想想生活当中工厂的作用(为我们生产新的商品),而在编程中工厂就是给我们创建(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:处理媒体类文件
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 @Override13 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 block41 e.printStackTrace();42 } catch (IOException e) {43 // TODO Auto-generated catch block44 e.printStackTrace();45 }finally{46 47 try {48 in.close();49 out.close();50 } catch (IOException e) {51 // TODO Auto-generated catch block52 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,pdf19 */20 public class UpWords implements IFold {21 22 @Override23 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 block57 e.printStackTrace();58 } catch (IOException e) {59 // TODO Auto-generated catch block60 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 block73 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 MapgetProperties(){13 Properties prop = new Properties();14 Map map = new HashMap ();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 block26 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.properties
1 jpg=com.cn.Imp.UpMadie2 png=com.cn.Imp.UpMadie3 mp4=com.cn.Imp.UpMadie4 mp3=com.cn.Imp.UpMadie5 wma=com.cn.Imp.UpMadie6 pdf=com.cn.Imp.UpWords7 doc=com.cn.Imp.UpWords8 txt=com.cn.Imp.UpWords9 docx=com.cn.Imp.UpWords
测试类:Text
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 }