jsp表单
<%@ page language="java" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <form action="cos.jsp" method="post" enctype="multipart/form-data"> <input type="text" name="info1"><br> <input type="text" name="info2"><br> <input type="file" name="userface1"><br> <input type="file" name="userface2"><br> <input type="file" name="userface3"><br> <input type="submit" name="mysub" value="upfile"> </form> </body> </html>
cos组件实现文件上传 jsp代码
<%@ page language="java" pageEncoding="utf-8"%> <%@ page import="java.io.File"%> <%@ page import="com.oreilly.servlet.multipart.FilePart"%> <%@ page import="com.oreilly.servlet.multipart.ParamPart"%> <%@ page import="com.oreilly.servlet.multipart.Part"%> <%@ page import="com.oreilly.servlet.multipart.MultipartParser"%> <% //使用cos组件实现文件上传 MultipartParser mp = new MultipartParser(request, 10 * 1024 * 1024); Part part; mp.setEncoding("utf-8");//取决于来源页面的编码,如果是表态的应为gbk,如果是jsp应为utf-8 int ii = 0; while ((part = mp.readNextPart()) != null) { String name = part.getName(); if (part.isParam()) { ParamPart paramPart = (ParamPart) part; String value = paramPart.getStringValue(); out.println("param: name=" + name + "; value=" + value); System.out.println(value); } else if (part.isFile()) { FilePart filePart = (FilePart) part; String fileName = filePart.getFileName(); if (fileName != null) { long size = filePart.writeTo(new File(this.getServletContext().getRealPath("/upload"))); out.println("file: name=" + name + "; fileName=" + fileName + ", filePath=" + filePart.getFilePath() + ", contentType=" + filePart.getContentType() + ", size=" + size); }else { out.println("file: name=" + name + "; EMPTY"); } out.print(fileName); } } %>
--------------------------------------------------------------------------------------------------------
文件命名机制
1.根据uuid
import java.io.File; import java.util.UUID; import com.oreilly.servlet.multipart.FileRenamePolicy; public class MyUuid implements FileRenamePolicy { @Override public File rename(File f) { String body = UUID.randomUUID().toString(); String ext = ""; int pot = f.getName().lastIndexOf("."); if (pot != -1) { ext = f.getName().substring(pot); } String newName = body + ext; f = new File(f.getParent(), newName); return f; } }
2.根据时间随机数
import java.io.File; import java.util.UUID; import com.oreilly.servlet.multipart.FileRenamePolicy; public class MyRandom implements FileRenamePolicy { @Override public File rename(File f) { String body = String.valueOf(System.currentTimeMillis()); String ext = ""; int pot = f.getName().lastIndexOf("."); if (pot != -1) { ext = f.getName().substring(pot); } String newName = body + ext; f = new File(f.getParent(), newName); return f; } }
3.上传文件并改名字
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>cc.html</title> </head> <body> <form action="cc.jsp" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 文件1:<input type="file" name="ufile1"><br> 文件2:<input type="file" name="ufile2"><br> <input type="submit" value="提交"> </form> </body> </html>
<%@page import="com.MyRandom"%> <%@ page language="java" pageEncoding="utf-8"%> <%@ page import="com.oreilly.servlet.MultipartRequest"%> <%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%> <%@ page import="com.MyUuid"%> <%@ page import="java.io.File"%> <% //使用cos组件实现文件上传 String path = this.getServletContext().getRealPath("/upload"); //MultipartRequest req = new MultipartRequest(request, path, "utf-8"); //out.print(req.getParameter("uname")); //out.print("<hr>"); //out.print(req.getFile("ufile1").getName()); //out.print(req.getFile("ufile2").getName()); //MultipartRequest req = new MultipartRequest(request,path,10*1024*1024,"utf-8",new MyUuid()); MultipartRequest req = new MultipartRequest(request,path,10*1024*1024,"utf-8",new MyRandom()); out.print(req.getFile("ufile1").getName()); %>
-------------------------------------------------------------------------------------------------------
用servlet技术用cos组建实现上传下载
一、JSP页面如下: