1. JSP
- <%@ page contentType="text/html;charset=GBK"%>
- <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
- <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
- <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
- <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
- <html:html>
- <head>
- <title>User List Page</title>
- </head>
- <body bgcolor="#99ffff">
- <center><br><br><br>
- <logic:present name="user" scope="session">
- <html:form action="user.do" method="post" enctype="multipart/form-data">
- <table border="2" bgcolor="#ffff99">
- <tr>
- <td>Upload: </td>
- <td><html:file property="upDoc" /></td>
- <td><html:text property="upDocName" /></td>
- </tr>
- <tr>
- <td colspan="3" align="center">
- <html:hidden property="status" value="upload" />
- <html:submit value="Upload" />
- <html:reset value="RESET" />
- </td>
- </tr>
- </table>
- </html:form>
- </logic:present>
- </center>
- </body>
- </html:html>
2. ActionForm
- private FormFile upDoc; //接收类型为org.apache.struts.upload.FormFile. 这个要特别关注, 因为批量上传时不能用这个类型了
- private String upDocName;
- public FormFile getUpDoc() {
- return upDoc;
- }
- public void setUpDoc(FormFile upDoc) {
- this.upDoc = upDoc;
- }
- public String getUpDocName() {
- return upDocName;
- }
- public void setUpDocName(String upDocName) {
- this.upDocName = upDocName;
- }
3. UploadAction
- public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) {
- userForm = (UserForm) form;
- String upDocName = userForm.getUpDocName();
- FormFile formFile = userForm.getUpDoc();
- OutputStream out = null;
- String uploadPath = null;
- // 在编写IO流的时候必须有一个明确的绝对路径 --> this.getServlet().getServletContext().getRealPath("/")取得了当前项目的绝对路径。
- /*
- * split()方法:public String[] split(String regex[, int limit]);
- * eg:str.split(regex); --> 返回一个下标从0开始的数组,regex不作为任何数组元素的部分返回。
- * 因为split()的参数regex是正则表达式, 所以特殊字符必须转义:
- * 1、如果用"."作为分隔的话,必须是如下写法:String.split("//."),这样才能正确的分隔开,不能用String.split(".");
- * 2、如果用"|"作为分隔的话,必须是如下写法:String.split("//|"),这样才能正确的分隔开,不能用String.split("|");
- * 3、如果用"/"作为分隔,就得写成这样:String.split(""),因为在Java中是用"//"来表示"/"的, "."、"|"和"/"都是转义字符,必须得加"//";
- * 4、如果在一个字符串中有多个分隔符,可以用"|"作为连字符,比如:"acount=? and uu =? or n=?",把三个都分隔出来,可以用String.split("and|or");
- * limit可选,用来限制返回数组中的元素个数。
- */
- if ("".equals(upDocName)) {
- //不修改上传文件名时uploadPath的值
- uploadPath = this.getServlet().getServletContext().getRealPath("/") + "upload//" + userForm.getUpDoc().getFileName();
- } else {
- uploadPath = this.getServlet().getServletContext().getRealPath("/") + "upload//" +
- userForm.getUpDocName() + "." + userForm.getUpDoc().getFileName().split("//.")[1];
- }
- try {
- byte b[] = formFile.getFileData();
- out = new FileOutputStream(new File(uploadPath));
- out.write(b);
- } catch (FileNotFoundException fnfe) {
- System.err.println(this.getClass().getName() + ": upload: failed --> " + fnfe.toString());
- } catch (IOException ioe) {
- System.err.println(this.getClass().getName() + ": upload: failed --> " + ioe.toString());
- } finally {
- try {
- out.close();
- } catch (IOException ioe) {
- System.err.println(this.getClass().getName() + ": upload - out.close() failed --> " + ioe.toString());
- }
- }
- return mapping.findForward("upload");
- }