图片上传:
效果图:
项目结构:
上传图片页面UploadImg.jsp:
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="上传">
</form>
上传图片成功页面success.jsp:
<input type="image" src="/UploadStruts2${imgPath }" alt="${imgName }" style="whith:200px;height:300px;">
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>UploadImg.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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>
<package name="default" extends="struts-default">
<!-- 图片上传action -->
<action name="upload" class="cn.xxs.action.UploadAction" method="upload">
<!-- 配置默认拦截器栈 -->
<interceptor-ref name="defaultStack">
</interceptor-ref>
<!-- 引入文件上传的拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 配置允许的文件格式 -->
<param name="allowedTypes">application/pdf,text/plain,image/bmp,image/png,image/jpg</param>
<!-- 文件大小 -->
<param name="maxmumSize">5242880</param>
</interceptor-ref>
<result name="success">success.jsp</result>
</action>
</package>
</struts>
BaseAction.java
package cn.xxs.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport{
/**
* 获取request
* @return
*/
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
/**
* 获取Response
* @return
*/
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
/**
* 获取HttpSession
*/
public HttpSession getSession() {
return getRequest().getSession();
}
}
UploadAction.java:
package cn.xxs.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class UploadAction extends BaseAction{
private File image;
private String imageFileName;
private String imageContentType;
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
/**
* 图片上传
*/
public String upload() {
//1.获取upload文件在服务器tomcat中的路径
String path = getSession().getServletContext().getRealPath("/upload");
//2.创建一个文件,需要传入路径
File file = new File(path);
//3.判断文件夹是否存在,不存在,则新建一个文件夹
if(file.exists()) {
file.mkdirs();
}
//4.获取图片在tomcat中的真实路径
String realPath = path + "/" + imageFileName;
//5.判断图片是否存在,存在则删除
File f = new File(realPath);
if(f.exists()) {
f.delete();
}
//6.把图片copy到tomcat中的upload文件夹中
try {
FileUtils.copyFile(image, f);
}catch(IOException e){
e.printStackTrace();
}
//7.可以获取图片名字在页面显示
String imgName = imageFileName.substring(0,imageFileName.lastIndexOf("."));
String imgPath = "/upload/"+imageFileName;
//8.把图片名字,路径返回到前台
getRequest().setAttribute("imgPath", imgPath);
getRequest().setAttribute("imgName", imgName);
return "success";
}
}
文件上传:
效果图:
项目结构:
上传文件页面index.jsp:
<form action="upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="myFile"><br>
<input type="submit" value="提交">
</form>
上传文件成功页面success.jsp:
fileName:${myFileFileName }<br>
myFileContentType:${myFileContentType }<br>
Name"${myFile.name }"
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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.custom.i18n.resources" value="UTF-8"></constant>
<!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 需要继承struts-default包,这样就拥有的最基本的功能 -->
<package name="upload" extends="struts-default">
<action name="upload" class="com.xxs.action.UploadFileAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
UploadFileAction.java:
package com.xxs.action;
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.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
public class UploadFileAction {
private String title;
//可以得到上传文件的名称
//规则:输入域的名称+固定字符串FileName
private String myFileFileName;
//取得文件数据
//规则:File 输入域的名称
private File myFile;
//取得内容类型
//规则:输入域的名称+固定字符串ContentType
private String myFileContentType;
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String execute(){
System.out.println("fileName:"+this.getMyFileFileName());
System.out.println("contentType:"+this.getMyFileContentType());
System.out.println("File:"+this.getMyFile());
//获取要保存文件夹的物理路径(绝对路径)
String realPath=ServletActionContext.getServletContext().getRealPath("/upload");
File file = new File(realPath);
//测试此抽象路径名表示的文件或目录是否存在。若不存在,创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
if(!file.exists())file.mkdirs();
try {
//保存文件
FileUtils.copyFile(myFile, new File(file,myFileFileName));
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
}