Struts文件上传
用到的包:一个是commons-fileupload-1.3.2.jar,另一个是commons-io-2.2.jar
第一步:做界面。
1.<input type="file">
2.<form enctype="multipart/form-data">
第二步:做Action。
三个成员:1.File类型 2.String fileFileName属性名 3.String fileContentType属性名
org.apache.commons.io.FileUtils.copyFile很简单。否则用IO流也可以操作。
用IO流操作
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File(root, fileFileName));
byte[] buffer = new byte[500];
int length = 0;
while(-1 != (length = is.read(buffer, 0, buffer.length)))
{
os.write(buffer);
}
os.close();
is.close();
现在我们就做一个简单的上传:
先做页面部分:
<%@ 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> </head> <body> <h1>文件上传</h1> <form action="Upload_upload" method="post" enctype="multipart/form-data"> 标题:<input type="text" name="title"><br> <input type="file" name="file"><br> <input type="submit" value="GO"> </form> </body> </html>
然后是Action部分:
package com.itnba.maya.controller; import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private String title; private File file; private String fileFileName; private String fileContentType; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String input(){ return SUCCESS; } public String upload() throws IOException{
//获取当前时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss"); Date cal = Calendar.getInstance().getTime(); String now = sdf.format(cal);
//获取四位随机数 Random rand = new Random(); int n = rand.nextInt(9999); DecimalFormat dec = new DecimalFormat("0000"); String sj = dec.format(n);
//生成地址 File ss = new File("d://"+now+sj+fileFileName); FileUtils.copyFile(file, ss); return SUCCESS; } }
这样就可以了,看下结果:
碰到的问题:
1.如何解决文件重名的问题?
加时间,随机数,用户名,序列号
2.如何解决大文件上传的问题?
在struts.xml 中添加常量。这个常量在struts.properties中(/org/apache/struts2/default.properties)。
<constant name="struts.multipart.maxSize" value="20971520"></constant>
3.怎么上传多个文件:
把相应的成员变成数组即可。
页面代码如下:
<%@ 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> </head> <body> <h1>文件上传</h1> <form action="Upload_upload" method="post" enctype="multipart/form-data"> 标题:<input type="text" name="title"><br> <input type="file" name="file"><br> <input type="file" name="file"><br> <input type="file" name="file"><br> <input type="file" name="file"><br> <input type="file" name="file"><br> <input type="file" name="file"><br> <input type="submit" value="GO"> </form> </body> </html>
Action代码:
package com.itnba.maya.controller; import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private String title; private File[] file; private String[] fileFileName; private String[] fileContentType; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public File[] getFile() { return file; } public void setFile(File[] file) { this.file = file; } public String[] getFileFileName() { return fileFileName; } public void setFileFileName(String[] fileFileName) { this.fileFileName = fileFileName; } public String[] getFileContentType() { return fileContentType; } public void setFileContentType(String[] fileContentType) { this.fileContentType = fileContentType; } public String input(){ return SUCCESS; } public String upload() throws IOException{ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss"); Date cal = Calendar.getInstance().getTime(); String now = sdf.format(cal); Random rand = new Random(); int n = rand.nextInt(9999); DecimalFormat dec = new DecimalFormat("0000"); String sj = dec.format(n); for(int i = 0 ;i<file.length;i++){ File ss = new File("d://"+now+sj+fileFileName[i]); FileUtils.copyFile(file[i], ss); } return SUCCESS; } }
看下我们的结果:
我们在这里只选取一部分添加看下能不能完成上传,
这样一个简单的上传就完成了,如果想用功能更加全面,界面更加好看的就需要引用js,jquery等插件去完成了,这里就不一一验证了