Struts2实现文件上传下载功能(批量上传)

今天来发布一个使用Struts2上传下载的项目,

struts2为文件上传下载提供了好的实现机制,

首先,可以先看一下我的项目截图

Struts2实现文件上传下载功能(批量上传)

关于需要使用的jar包,需要用到commons-fileupload-1.3.1.jar,commons-io-2.2.jar包,有想要的可以联系我,

1. 现在让我们来看一下jsp界面:(使用了js,完成批量上传的功能)

Struts2实现文件上传下载功能(批量上传)

具体代码如下(NewFile.jsp):

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript">
function addfile() {
var i = document.createElement("input");
i.setAttribute("type","file");
i.setAttribute("name","doc"); var d = document.createElement("div"); document.getElementById("ll").appendChild(i);
document.getElementById("ll").appendChild(d);
}
</script>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<div id="ll">
上传<input type="file" name="doc" /><br/>
</div>
<a href="javascript:addfile()">继续上传</a>
<input type="submit" value="上传" />
</form>
</body>
</html>

2. form表单提交至upload.action代码如下:

 package com.llh.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Random; import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 1L; private File doc[]; private String docFileName[]; public File[] getDoc() {
return doc;
} public void setDoc(File[] doc) {
this.doc = doc;
} public String[] getDocFileName() {
return docFileName;
} public void setDocFileName(String[] docFileName) {
this.docFileName = docFileName;
} @Action(value = "upload", interceptorRefs = { @InterceptorRef(value = "fileUpload", params = { "allowedExtensions",
".jpg,.txt", "maximumSize", "2000000" }), @InterceptorRef(value = "defaultStack") }, results = {
@Result(name = "success", location = "/success.jsp"),
@Result(name = "input", location = "/error.jsp") })
public String execute() { for (int i = 0; i < doc.length; i++) {
File doc1 = doc[i];
String docFileName1 = docFileName[i]; String path = ServletActionContext.getServletContext().getRealPath("/upload/" + changeName(docFileName1));
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(doc1);
fos = new FileOutputStream(path);
byte[] b = new byte[1024];
int length = fis.read(b);
while (length != -1) {
fos.write(b, 0, length);
length = fis.read(b);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return SUCCESS;
} public String changeName(String oldName) {
int index = oldName.indexOf(".");
String name = oldName.substring(0, index);
String extension = oldName.substring(index, oldName.length());
Date d = new Date();
Random r = new Random();
return name + d.getTime() + r.nextInt(999999) + extension; } }
上一篇:JavaScript中事件绑定的方法总结


下一篇:最NB的发现 LINUX 下玩teamviewer 命令行设置密码