struts提供了<html:file>标签,集成了Apache的commons-upload工具包,上传文件变得更加简单,不再需要解析Request、遍历所有的输入域、判断是否为文件等。
struts对Apache的commons-upload进行了再封装,把上传文件封装成FormFile对象,直接获取该对象,将文件数据保存即可。
代码1:UploadForm.java
package com.strust1.test.actionFrom; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; public class UploadForm extends ActionForm { private FormFile file; private String action; private String text; public FormFile getFile() { return file; } public void setFile(FormFile file) { this.file = file; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
代码2:upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <html:form action="/upload?action=upload" enctype="multipart/form-data"> 文件:<html:file property="file" style="width:200px;"></html:file><br/> 备注:<html:textarea property="text" style="width:200px"></html:textarea><br/> <html:submit value="开始上传"></html:submit> </html:form> </body> </html>
注意:上传文件时Form表单要指定enctype为multipart/form-data,method一定要设置为POST。<html:form/>标签默认为POST。
FormFile并不是普通的文件,不能直接保存。FormFile提供getInputStream()接口,可以获得它的输入流,将它的内容保存到指定的文件中即可。
代码3:UploadAction.java
package com.strust1.test.action; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.strust1.test.actionFrom.UploadForm; public class UploadAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UploadForm uploadForm = (UploadForm)form; if("upload".equals(uploadForm.getAction())){ return upload(mapping, form, request, response); } return mapping.getInputForward(); } public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{ UploadForm uploadForm =(UploadForm)form; StringBuffer buffer = new StringBuffer(); buffer.append("<style> body{font-size:12px;}</style>"); if(uploadForm.getFile() != null && uploadForm.getFile().getFileSize() > 0){ File classes = new File(getClass().getClassLoader().getResource("").getFile());//获得文件夹/WEB-INF/Classes File uploadFolder = new File(classes.getParentFile().getParentFile() , "upload");//获取文件夹webRoot下的文件夹/upoad uploadFolder.mkdirs();//创建文件夹 File file = new File(uploadFolder, uploadForm.getFile().getFileName());//保存到 /upload/下面 System.out.println(file.getPath()); OutputStream ous = null; InputStream ins =null; try{ byte[] b = new byte[1024]; int len = 0; ins = uploadForm.getFile().getInputStream(); ous = new FileOutputStream(file); while((len = ins.read(b)) != -1){ ous.write(b ,0 ,len); } }finally{ ins.close(); ous.close(); } buffer.append("文件:<a href=upload/" + file.getName() + "target=_blank>" + file.getName() + "</a><br/>"); }else{ buffer.append("文件: 没有选择文件<br/>"); } buffer.append("备注:" + uploadForm.getText() + "<br/>"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(buffer.toString()); return null; } }
文件保存在本Web应用的/upload文件夹下。