将struts1文件上传的操作汇总了一下,包括单文件上传和多文件上传,内容如下,留作备忘:
Struts2实现文件上传的文章(http://blog.csdn.net/itwit/article/details/7031785)
1、单文件上传
1)JSP页面(singleFileUpload.jsp)
<%@page pageEncoding = "UTF-8"%> <h1>
文件上传
</h1>
<hr />
<form action="upload.action" method="post" enctype="multipart/form-data">
name:
<input type="text" name="name" />
<br />
upload:
<input type="file" name="file" />
<br />
<input type="submit" value="上传" />
</form>
操作点:enctype="multipart/form-data"
2)Form(UploadForm)
package fileUpload.singleFileUpload.form; import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile; public class UploadForm extends ActionForm { private static final long serialVersionUID = 1L; private String name; private FormFile file; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public FormFile getFile() {
return file;
} public void setFile(FormFile file) {
this.file = file;
}
}
操作点:Struts1提供了一个FormFile类来处理文件上传操作,另外注意,属性name,file名字必须与jsp页面一致,否则会报空指针。
3)Action(UploadAction)
package fileUpload.singleFileUpload.action; import java.io.FileOutputStream;
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 org.apache.struts.upload.FormFile; import fileUpload.singleFileUpload.form.UploadForm; public class UploadAction extends Action { // @Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UploadForm uf = (UploadForm)form;
String name = uf.getName();
FormFile file = uf.getFile();
System.out.println("name=" + name);
String filename = file.getFileName();
System.out.println("fileName=" + filename); FileOutputStream fos = new FileOutputStream("d:\\["+name+"]" + filename); //创建输出流
fos.write(file.getFileData()); //写入
fos.flush();//释放
fos.close(); //关闭
return mapping.findForward("success");
}
}
操作点:FormFile类的方法
4)Struts配置文件(struts-config.xml)
<!-- 配置 单个上传 -->
<form-bean name="uploadForm" type="fileUpload.singleFileUpload.form.UploadForm" />
<!-- 配置 单个上传 -->
<action path="/upload" type="fileUpload.singleFileUpload.action.UploadAction" name="uploadForm" scope="request">
<forward name="success" path="/singleFileUpload.jsp" />
</action>
<!-- 配置文件上传大小 -->
<!-- 规定了最大上传大小是2M,如果设置成-1则大小无限制了 -->
<controller maxFileSize="2M" />
操作点:controller maxFileSize用来配置上传文件的大小,可根据实际情况配置,设置成-1则表示无限制。其位置必须放在</action-mappings>和<message-resources>之间
2、多文件上传
1)JSP页面(mutityFileUpload.jsp)(从网上获得的代码,可增可减,不错)
<%@ page pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>上传任意多个文件(总大小不能超过2M)</title>
<script language="javascript">
// 在DOM中插入一个上传文件列表项(div元素)和一个<input type="file"/>元素
function insertNextFile(obj)
{
// 获取上传控制个数
var childnum = document.getElementById("files").getElementsByTagName("input").length;
var id = childnum - 1;
var fullName = obj.value;
// 插入<div>元素及其子元素
var fileHtml = '';
fileHtml += '<div id = "file_preview' + id + '" style ="border-bottom: 1px solid #CCC;">';
fileHtml += '<img width =30 height = 30 src ="images/file.gif" title="' + fullName + '"/>';
fileHtml += '<a href="javascript:;" onclick="removeFile(' + id + ');">删除</a> ';
fileHtml += fullName.substr(fullName.lastIndexOf('\\')+1) +' </div>'; var fileElement = document.getElementById("files_preview");
fileElement.innerHTML = fileElement.innerHTML + fileHtml;
obj.style.display = 'none'; // 隐藏当前的<input type=”file”/>元素
addUploadFile(childnum); // 插入新的<input type=”file”/>元素
}
// 插入新的<input type=”file”/>元素,适合于不同的浏览器(包括IE、FireFox等)
function addUploadFile(index)
{
try // 用于IE浏览器
{
var uploadHTML = document.createElement( "<input type='file' id='file_" + index +
"' name='file[" + index + "]' onchange='insertNextFile(this)'/>");
document.getElementById("files").appendChild(uploadHTML);
}
catch(e) // 用于其他浏览器
{
var uploadObj = document.createElement("input");
uploadObj.setAttribute("name", "file[" + index + "]");
uploadObj.setAttribute("onchange", "insertNextFile(this)");
uploadObj.setAttribute("type", "file");
uploadObj.setAttribute("id", "file_" + index);
document.getElementById("files").appendChild(uploadObj);
}
}
function removeFile(index) // 删除当前文件的<div>和<input type=”file”/>元素
{
document.getElementById("files_preview").removeChild(document.getElementById("file_preview" + index));
document.getElementById("files").removeChild(document.getElementById("file_" + index));
}
function showStatus(obj) // 显示“正在上传文件”提示信息
{
document.getElementById("status").style.visibility="visible";
}
</script>
</head>
<body>
<html:form enctype="multipart/form-data" action="multiUpload">
<span id="files"> <%-- 在此处插入用于上传文件的input元素 --%>
<input type="file" id="file_0" name="file[0]" onchange="insertNextFile(this)" /> </span>
<html:submit value=" 上传 " onclick="showStatus(this);" />
</html:form>
<p>
<div id="status" style="visibility: hidden; color: Red">
正在上传文件
</div>
<p>
<%-- 在此处用DOM技术插入上传文件列表项 --%>
<div id="files_preview"
style="width: 500px; height: 500px; overflow: auto"></div>
</body>
</html>
2)Form(UploadMoreForm)
package fileUpload.mutityFileUpload.form; import java.util.ArrayList;
import java.util.List; import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile; public class UploadMoreForm extends ActionForm{ private static final long serialVersionUID = 1L;
private List<FormFile> myFiles = new ArrayList<FormFile>(); // 用于保存不定数量的FormFile对象 public FormFile getFile(int i) // 索引属性
{
return myFiles.get(i);
}
public void setFile(int i, FormFile myFile) // 索引属性
{
if (myFile.getFileSize() > 0) // 只有上传文件的字节数大于0,才上传这个文件
{
myFiles.add(myFile);
}
}
public int getFileCount() // 获得上传文件的个数
{
return myFiles.size();
} }
3)Action(UploadMoreAction)
package fileUpload.mutityFileUpload.action; import java.io.FileOutputStream; 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 org.apache.struts.upload.FormFile; import fileUpload.mutityFileUpload.form.UploadMoreForm; public class UploadMoreAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
UploadMoreForm umForm = (UploadMoreForm) form;
int count = 0;
try
{
count = umForm.getFileCount(); // 获得上传文件的总数
for (int i = 0; i < count; i++)
{
FormFile file = umForm.getFile(i);
System.out.println(file.getFileName());
FileOutputStream fos = new FileOutputStream("d:\\" + file.getFileName()); //创建输出流
fos.write(file.getFileData()); //写入
fos.flush();//释放
fos.close(); //关闭 }
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
} }
4)Struts配置文件(struts-config.xml)
<!-- 配置 多个上传 -->
<form-bean name="uploadMoreForm" type="fileUpload.mutityFileUpload.form.UploadMoreForm" />
<!-- 配置 多个上传 -->
<action path="/multiUpload" type="fileUpload.mutityFileUpload.action.UploadMoreAction" name="uploadMoreForm" scope="request">
<forward name="success" path="/mutityFileUpload.jsp" />
</action>
<!-- 配置文件上传大小 -->
<!-- 规定了最大上传大小是2M,如果设置成-1则大小无限制了 -->
<controller maxFileSize="-1" />