jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>上传用户信息</title>
</head>
<body>
<s:fielderror cssStyle="color:red;"></s:fielderror>
<form action="upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="userImg"/><br>
<input type="submit" value="上传">
</form>
</body>
</html>
struts配置文件struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 上传文件的大小 还有一个总的控制。 -->
<constant name="struts.multipart.maxSize" value="2097152"></constant>
<package name="demo" extends="struts-default">
<action name="upload" class="org.lzh.action.UploadAction" method="doupload">
<result name="success">ok.jsp</result>
<!-- 如果上传的文件不符合param的要求会将错误信息返回页面 -->
<result name="input">upload.jsp</result>
<!-- 配置fileUpload 内置拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 配置上传文件的类型 -->
<param name="allowedTypes">image/png,image/jpeg,image/pjpeg,image/gif</param>
<!-- 配置上传文件的大小 (字节) 会报错。-->
<param name="maximumSize">200</param>
</interceptor-ref>
<!-- 一旦显示的配置了拦截器,一定要再显示的配置一遍默认的拦截器。 -->
<interceptor-ref name="defaultStack"/>
</action>
</package>
</struts>
Action层的设置
package org.lzh.action;
import java.io.File;
import java.util.Calendar;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.lzh.util.IOUtil;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 4304707622756855561L;
//struts2中对上传文件的参数写法。有要求。
private File userImg;//上串的文件,要与页面上传文件名字保持一致
private String userImgFileName;//上传的文件的文件名称,将上传文件后加上FileName
private String userImgContentType;//上传文件的类型,将上传的文件后加上ContentType
private String filePath;//为了前台页面展示设置文件的路径
public File getUserImg() {
return userImg;
}
public void setUserImg(File userImg) {
this.userImg = userImg;
}
public String getUserImgFileName() {
return userImgFileName;
}
public void setUserImgFileName(String userImgFileName) {
this.userImgFileName = userImgFileName;
}
public String getUserImgContentType() {
return userImgContentType;
}
public void setUserImgContentType(String userImgContentType) {
this.userImgContentType = userImgContentType;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String doupload()throws Exception{
/**
* 相对路径:相对于一个路径(如项目的跟路径)算起的路径叫相对路径。
* 绝对路径:从盘符开始算起的路径叫绝对路径。
*/
//1,取得项目的跟路径(可使用request.realPath()方法)
/*HttpServletRequest request=ServletActionContext.getRequest();
String realPath=request.getRealPath("/");*/
//2,取得项目的跟路径(可使用application.realPath()方法)
ServletContext applicateion= ServletActionContext.getServletContext();
String realpath=applicateion.getRealPath(File.separator);
//将文件重命名
Calendar calendar=Calendar.getInstance();
String toFileName=calendar.getTimeInMillis()+userImgFileName.substring(userImgFileName.lastIndexOf("."));
filePath="images"+File.separator+toFileName;
String path=realpath+File.separator+filePath;
File toFile=new File(path);
IOUtil.readAndWrite(userImg,toFile);
return "success";
}
}
IOUtil下的readAndRead方法
package org.qrsx.util;
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.IOException;
import java.io.Serializable;
/**
* IO读写工具类
* @author lzh
*
*/
public class IOUtil implements Serializable {
private static final long serialVersionUID = 4034287994353806903L;
private static BufferedInputStream bis=null;
private static BufferedOutputStream bos=null;
/**
* 基于Buffer的读写。
* @param fromFile 要读取的文件。
* @param toFile 要写入的文件。
*/
public static void readAndWrite(File fromFile,File toFile){
try {
//得到一个输入流。
bis=new BufferedInputStream(new FileInputStream(fromFile));
//输出流
bos=new BufferedOutputStream(new FileOutputStream(toFile));
//开始从输入流中读。用输出流写。
//定义一个byte数组,临时保存读取的资源。
byte[]bytes=new byte[2048];
//定义一个lenght ,保存读取的资源的个数。
int length=0;
//判断 ,如果读取到资源的末尾则 返回-1,否则返回读取的资源的个数。
while((length=bis.read(bytes))!=-1){
//写出,从byte数组中写出,从0开始,写到length个长度。
bos.write(bytes, 0, length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
//判断输入流和输出流是否已经关闭,如果没有关闭就close
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}