strut2 文件上传完整案例
之前写的文章都是关键的代码,今天整理好一个完整案例,粘贴可运行。
上传界面:upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <form action="upload.action" method="post" enctype="multipart/form-data"> 文件:<input type="file" name="upload"/> <input type="submit" value="上传"/> </form> </center> </body> </html>
action中的代码:FileUploadAction.java
package cn.itcast.action; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport{ private String savefile;//通过注入得到上传的路径 private File upload; private String uploadFileName; 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 getSavefile() { return savefile; } public void setSavefile(String savefile) { this.savefile = savefile; } //文件拷贝函数 //src表示源文件,dst表示目标文件 public static void copy(File src,File dst) throws Exception{ //定义一个输入流对象,使用缓冲类需要提供一个输入流对象 InputStream in = new BufferedInputStream(new FileInputStream(src)); OutputStream out = new BufferedOutputStream(new FileOutputStream(dst)); //声明一个字节数组,用来存储读入的数据 byte[] b = new byte[1024]; int length=0; //循环读取输入流对象,把输入流读到字节数组中,返回读取的字节数长度,如果读到末尾则返回-1 while(-1!=(length=in.read(b))){ out.write(b);//讲字节数组的数据写到输出流文件中. } in.close(); //如果不关闭的话上传的文件将无法正常打开!! out.close(); } public String execute(){ String path = ServletActionContext.getServletContext().getRealPath(getSavefile()+"/"); File parfiFile = new File(path);//目录路径 if(!parfiFile.exists()){ parfiFile.mkdir(); } File file = new File(parfiFile, getUploadFileName());//通过父文件+文件名得到一个实际的文件 if(file.getParent()!=null){ try { copy(upload, file);//调用文件拷贝的方法 System.out.println("上传成功"); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("上传失败"); e.printStackTrace(); } } return "success"; } }
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.i18n.encoding" value="utf-8"></constant> <constant name="struts.multipart.saveDir" value="F:\"></constant> <constant name="struts.multipart.maxSize" value="10240000" /> <package name="base" extends="struts-default"> <global-results> <result name="message">/WEB-INF/page/message.jsp</result> </global-results> </package> <package name="itcast" extends="base" > <action name="upload" class="cn.itcast.action.FileUploadAction"> <interceptor-ref name ="fileUpload"> <param name ="allowedTypes" > image/bmp,image/png,image/gif,image/jpeg, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, application/msword </param > <param name="maximumSize">10240000</param> </interceptor-ref> <interceptor-ref name ="defaultStack" /> <param name="savefile">/uploadfile</param><!--注入上传的路径--> <result name="success">/index.jsp</result> </action> </package> </struts>