《软件架构课程设计》由会员分享,可在线阅读,更多相关《软件架构课程设计(10页珍藏版)》请在金锄头文库上搜索。
1、软件架构课程设计软件架构课程设计电子相册管理系统专业:软 件 工 程姓名:樊 帆学号:200705070313package hbu.david.cmc.util; /* 通用文件上传类,对 FileUpload 的包装*/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException;
2、import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID;import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; impor
3、t javax.servlet.http.HttpServletResponse;import mons.fileupload.FileItem; import mons.fileupload.FileItemFactory; import mons.fileupload.FileUpload; import mons.fileupload.FileUploadException; import mons.fileupload.disk.DiskFileItemFactory; import mons.fileupload.servlet.ServletFileUpload;import hb
4、u.david.cmc.http.ServletEncoding; import hbu.david.cmc.util.*; import hbu.david.cmc.bean.*;public class FileUploadUtil /* 初始化,获取 FileItem 的列表并返回*/ public static List init(HttpServletRequest request,HttpServletResponse response) ServletEncoding.setEncoding(request, response); List listFileItem=new Ar
5、rayList();/Map fileUpInfo = new HashMap();/List list = new ArrayList(); FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(factory); servletFileUpload.setFileSizeMax(4096 * 4096); try / 获取 request 中包含的 FileItems listFileItem = servletFile
6、Upload.parseRequest(request); catch (FileUploadException e) System.out.print(“size is too big “); return listFileItem; /* 获取文件域信息保存在 Map* * param fileItems* return*/ public static Map getFile(List fileItems) Map mapFiles = new HashMap(); for (int i = 0; i getField(List fileItems) Map mapFields = new
7、 HashMap(); for (int i = 0; i saveAs(HttpServletRequest request,Map mapFile, File pathDir) Map saveAsMap = new HashMap(); / 获取 Key 值的集合并且迭代,从 Map 中取出 FileItem Iterator i=mapFile.keySet().iterator(); System.out.println(mapFile.keySet(); while(i.hasNext() /取出 Map 中的映射 String fieldName = i.next(); File
8、Item fileItem = mapFile.get(fieldName); / 将文件另存为 pathDir String path = FileUtil.changePathToRelative(request, FileUploadUtil.saveAs(fileItem, pathDir); saveAsMap.put(fieldName, path); return saveAsMap; /* */ package hbu.david.cmc.util;import java.io.File;import javax.servlet.http.HttpServletRequest;
9、/* author Administrator* */ public class FileUtil public FileUtil() / TODO Auto-generated constructor stub /* 创建一个新的目录 如果创建成功返回这个新目录 创建失败就返回 null* * param file* return*/ public static File createFileDir(File file) File f; if (file.exists() / 路径已经存在 f = file; else / 路径不存在 if (file.mkdirs() / 如果创建成功 f
10、 = file; else / 创建失败 f = null; return f; /* 获取文件后缀名 并且改为小写*/ public static String getFileSuffix(String fileName) String suffix; suffix = fileName.trim().substring(fileName.trim().lastIndexOf(“.“) + 1) .toLowerCase(); return suffix; /* 改绝对路径为相对路径 注意是相对 web 应用名字的路径* * param request* param path* return
11、*/ public static String changePathToRelative(HttpServletRequest request, String path) String relativePath = “; String contextPath = request.getContextPath(); / 获取该路径在 Web 中的路径 String webPath = path.substring(path.lastIndexOf(contextPath .substring(1); / 从 web 路径中获取相对路径 relativePath = webPath.substri
12、ng(request.getContextPath().length(); return relativePath; /* 构造一个绝对路径*/ public static File changePathToAbsol(HttpServletRequest request, String path) return FileUtil.createFileDir(new File(request.getRealPath(“)+changePath(path); /* 替换路径中的/和*/ public static String changePath(String path) return pat
13、h.replace(“/“, File.separator).replace(“, File.separator); /* 删除一个文件,如果是目录就删除目录和目录下的所有文件*/ public static boolean deleteFile(File file) boolean ok=true; if(file.isFile() /文件 if(file.exists() file.delete(); else if(file.isDirectory() /目录,递归删除 System.out.println(file.getName(); if(file.listFiles()=null
14、|file.listFiles().length=0) file.delete(); else File f=file.listFiles(); for(int i=0;if.length;i+) deleteFile(fi);System.out.println(fi.getName(); file.delete(); return ok; /* param args*/ public static void main(String args) / TODO Auto-generated method stub File file = new File(“C:/TEST/“, “t.txt“
15、); createFileDir(file); System.out.print(deleteFile(file); package hbu.david.cmc.util;import javax.servlet.http.HttpServletRequest;/* author Administrator* */ public class ParseRequest /* */ public ParseRequest() / TODO Auto-generated constructor stub /* 从 request 中获取整数* param request* param parseString* param defaultNum* return*/ public static int parseInt(HttpServletRequest request, String parseString) return Integer.parseInt(request.getParameter(parseString); public static int parseInt(HttpServletReq