注:如果代码中的配置有些看不懂的,可以去看我之前写的Struts2配置,其他的我都会做出解释
1、全局图片
2、struts.xml (主配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 这是设置你上传文件的大小的常量配置 这两点都必须写上,不然你上传的文件稍微大点都会报错 -->
<constant name="struts.multipart.maxSize" value="100000000000"></constant>
<package name="struts-upload" namespace="/" extends="struts-default">
<!-- 全局结果集配置,如果返回error就从定向到error.jsp页面 -->
<global-results>
<result name="error" type="redirect">/error.jsp</result>
</global-results>
<action name="StrutsUploadAction" class="StrutsUploadAction" method="execute">
<!-- 这是设置你上传文件的大小的配置 -->
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">500000000</param>
</interceptor-ref>
<result name="success" type="redirect">/success.jsp</result>
</action>
</package>
</struts>
3、error.jsp (上传失败的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>
</head>
<body>
<h1>上传失败</h1>
</body>
</html>
4、success.jsp (上传成功的jsp)
4、success.jsp (上传成功的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>
</head>
<body>
<h1>上传成功</h1>
</body>
</html>
5、upload.jsp (上传jsp,这里说一点:form的enctype值必须是multipart/form-data)
<%@ 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>
<form action="${pageContext.request.contextPath }/StrutsUploadAction" method="post" enctype="multipart/form-data">
<input type="file" name="upload"> <br/>
<input type="submit" value="上传">
</form>
</body>
</html>
6、StrutsUploadAction.java (这里先给出最基础的Action,最好是看到这里停下来,然后用上面的代码 + 这个Action就自己去写出上传的全部,如果是会简单的java上传文件,那么你就可以写出来了)
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class StrutsUploadAction extends ActionSupport{
private File upload; //得到上传的文件
private String uploadContentType; //得到上传文件的类型
private String uploadFileName; //得到上传文件的名称
@Override
public String execute() throws Exception {
System.out.println("filename :" + this.getUploadFileName());
System.out.println("uploadType :" + this.getUploadContentType());
System.out.println("file" + this.getUpload());
return SUCCESS;
}
//get / set
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
7、如果看到这里你还是写不出完整的上传,也没有关系。下面我会把完整的Action给你,你可以参考的看看 (代码亲测有效)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class StrutsUploadAction extends ActionSupport{
private File upload; //得到上传的文件
private String uploadFileName; //得到上传文件的名称
@Override
public String execute() throws Exception {
//这个upload的路径是相对于WebContent下的
String path = ServletActionContext.getServletContext().getRealPath("/upload");
//准备这个文件的文件名
String filename = UUID.randomUUID().toString() + "." +this.getUploadFileName().substring(this.getUploadFileName().lastIndexOf(".")+1);
OutputStream out = new FileOutputStream(path + "/" + filename);
InputStream in = new FileInputStream(upload);
IOUtils.copy(in, out);
in.close();
out.close();
return SUCCESS;
}
//get / set
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
如果还有问题,可以给我留言或者联系我,我会尽可能的帮你解决的。