开发工具:Myeclipse6.5
web容器:Tomcat6
使用框架:Struts2.1.8
引入jar包(在Struts2的源码包里可以找到struts-2.1.8.1\lib):
上传页代码upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'upload.jsp' starting page</title> </head> <body> <center> <form action="upload.action" method="POST" enctype="multipart/form-data"> 文件标题:<input type="text" name="title" size="50" /> <br /> 选择文件: <input type="file" name="upload" size="50" /> <br /> <input type="submit" value=" 上传 " /> </form> </center> </body> </html>
action代码,UploadAction.java
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private static final int BUFFER_SIZE = 16*1024; private String title; private File upload; private String uploadFileName; private String imageName; private String uploadContentType; private String savePath; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } 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; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public static void copy(File src, File dst){ InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; int len = 0; while((len = in.read(buffer)) > 0){ out.write(buffer, 0, len); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(null != in){ try{ in.close(); }catch(IOException e){ e.printStackTrace(); } } if (null != out) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } public String execute() throws Exception{ String dstPath = ServletActionContext.getServletContext() .getRealPath(this.getSavePath()) + "\\" + this.getUploadFileName(); System.out.print("上传的文件的类型:" + this.getUploadContentType()); File dstFile = new File(dstPath); copy(this.upload,dstFile); return SUCCESS; } }
上传成功跳转页showUpload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'showUpload.jsp' starting page</title> </head> <body> <div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" > <img src ='upload/<s:property value ="uploadFileName" /> ' /> <br> <s:property value = "title"/> </div > </body> </html>
struts.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="upload" class = "com.wang.action.UploadAction"> <param name="savePath" >/upload</param> <result name= "success">/showUpload.jsp</result> </action> </package> </struts>