数据库:MySQL
开发技术:JSP + Servlet 2.5
第三方的上传组件:
commons-fileupload
connons-io
上传页面
1、form表单需要增加:enctype="multipart/form-data" 以字节流形式
2、form表单里面增加上传组件:input="file"
3、取消上传:<button onclick="javascript:window.opener == null;window.close();">取消上传</button>
<form id="form1" method="post" action="upload" enctype="multipart/form-data"> <table> <tr> <td width="25%" align="right">上传文件:</td> <td><input id="file1" type="file" NAME="file1" style=" 300px;"></td> </tr> <tr align="center" valign="middle"> <td height="60" colspan="2"> <input type="submit" id="BtnOK" value="确认上传"> <button onclick="javascript:window.opener == null;window.close();">取消上传</button> </td> </tr> <tr align="center" valign="middle"> 测试非表单控件的值接收:<td height="60" colspan="2"><input type="text" name="possess" value="非提交控件的值"></td> </tr> </table> </form>
代码:
工具类 MutiFileUpload.java 对fileupload组件进行简单封装
import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * 当读取上传表单的各部分时会用到该encoding,如果没有指定 * encoding * 则使用系统缺省的encoding。建议在这里设置成utf-8,并把jsp * 的charset也设置成utf-8,否则可能会出现乱码。 * */ @SuppressWarnings("unchecked") public class MutiFileUpload{ public Map<String,String> parameters ; //保存form表单域中非上传控件的值 public Map<String,FileItem> files; //保存上传的文件
//将文件保存在内存还是磁盘临时文件夹的默认临界值,值为10240,即10kb private int sizeThreshold = DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD; private long sizeMax = 1024 * 1024 * 20 ; //上传文件的大小限制; private String encoding = "UTF-8"; //字符编码,当读取上传表单的各部分时会用到该encoding
// 以上属性的 get/set 方法 public String getEncoding() { return encoding; } public void setEncoding(String encoding) { this.encoding = encoding; } public long getSizeMax() { return sizeMax; } public void setSizeMax(long sizeMax) { this.sizeMax = sizeMax; } public int getSizeThreshold() { return sizeThreshold; } public void setSizeThreshold(int sizeThreshold) { this.sizeThreshold = sizeThreshold; }
// 解析请求,这个方法很重要 public void parse(HttpServletRequest request){
//保存数据的2个Map初始化 parameters = new HashMap<String,String>(); files = new HashMap<String,FileItem>(); DiskFileItemFactory factory = new DiskFileItemFactory(); //设置临界值约束 factory.setSizeThreshold(sizeThreshold); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(sizeMax); upload.setHeaderEncoding(encoding); try { List items = upload.parseRequest(request); Iterator iterator = items.iterator(); while(iterator.hasNext()){ FileItem item = (FileItem)iterator.next(); if(item.isFormField()){ String fieldName = item.getFieldName(); String value = new String(item.getString(encoding)); parameters.put(fieldName, value); }else{ String fieldName = item.getFieldName(); files.put(fieldName, item); } } } catch (FileUploadException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** 得到上传文件的文件名 * @param item * @return */ public String getFileName(FileItem item){ String fileName = item.getName(); fileName = replace(fileName,"\","/"); fileName = fileName.substring(fileName.lastIndexOf("/")+1); return fileName; } /**字符串替换 * @param source * @param oldString * @param newString * @return */ public static String replace(String source, String oldString, String newString) { StringBuffer output = new StringBuffer(); int lengthOfSource = source.length(); int lengthOfOld = oldString.length(); int posStart = 0; int pos; while ((pos = source.indexOf(oldString, posStart)) >= 0) { output.append(source.substring(posStart, pos)); output.append(newString); posStart = pos + lengthOfOld; } if (posStart < lengthOfSource) { output.append(source.substring(posStart)); } return output.toString(); } }
上传Servlet,doGet方法请求doPost。在doPost方法中编写如下代码:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DBUtils db = new DBUtils(); MutiFileUpload fileUpload = new MutiFileUpload(); fileUpload.parse(request);
//这里是打印非上传组件的值,查看是否能够正常接收 //System.out.println( fileUpload.parameters.get("possess") ); Iterator<FileItem> iterator = fileUpload.files.values().iterator(); while(iterator.hasNext()){ FileItem item = iterator.next(); String fileName = fileUpload.getFileName(item); if( fileName != null && !fileName.equals("")){ //获取服务器的路径 String serverPath = request.getSession().getServletContext().getRealPath(""); //对上传文件重新命名 String rename = getReName(fileName); File file = new File( serverPath+"/upload/"+rename); try {
if( !file.isDirectory()){
//上传成功后做数据库操作 item.write(file); //数据库操作,这里没有考虑效率,如果是多文件上传,可以考虑SQL的批处理 db.saveOrUpdateOrDel("insert into uploadfile(realname,rename_) values(?,?)", new Object[]{fileName ,rename }); } } catch (Exception e) { e.printStackTrace(); //将上传的文件删除 if( file.exists()) file.delete(); } } } } /** * 以服务器当前时间为为上传文件重新命名 * @param fileName * @return 新文件名 */ private String getReName(String fileName){ int begin = fileName.lastIndexOf("."); String extend = fileName.substring(begin, fileName.length()); return System.currentTimeMillis() + extend; }
显示文件下载列表Servlet ,实体类就不再发了,可以在最下面按照SQL语句写
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DBUtils db = new DBUtils(); List<UploadFile> ufs = db.queryForObject("select * from uploadfile", null, UploadFile.class); request.setAttribute("filelist", ufs); request.getRequestDispatcher("filelist.jsp").forward(request,response); }
显示文件下载列表页面
╮(╯▽╰)╭ 循环部分发出来就行了,这个filelist.jsp页面本来就这么一点东西
<c:forEach items="${filelist}" var="uf"> ${uf.realname } <a href="down?re=${uf.rename_}&real=${uf.realname}">下载</a><br> </c:forEach>
文件下载Servlet
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取下载文件的名字 String re = request.getParameter("re"); //获取下载文件的真实名字 String real = request.getParameter("real"); //获取服务器路径 String serverPath = request.getSession().getServletContext().getRealPath(""); File f = new File( serverPath+"/upload/"+re); if( f.exists() ){ FileInputStream fis = new FileInputStream(f); //手动转码,示例中是GET请求,要么手动转码,要么在过滤器中进行GET请求处理,当然能在服务器直接配置get请求编码就一劳永逸了 String filename = new String(real.getBytes("ISO8859-1"),"UTF-8"); byte[] b = new byte[fis.available()]; fis.read(b); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition","attachment; filename="+filename ); //获取响应报文输出流对象 ServletOutputStream out =response.getOutputStream(); out.write(b); out.flush(); out.close(); } }
MySQL 数据库部分
SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for `uploadfile` -- ---------------------------- DROP TABLE IF EXISTS `uploadfile`; CREATE TABLE `uploadfile` ( `id` int(11) NOT NULL AUTO_INCREMENT, `realname` varchar(255) NOT NULL COMMENT '上传前的文件名字', `rename_` varchar(255) NOT NULL COMMENT '重新命名', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; SET FOREIGN_KEY_CHECKS = 1;