struts2文件上传

upload.html


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>简单的文件上传</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br>[code="java"]

选择文件:<input type="file" name="upload" /><br>
<input value="上传" type="submit" />
</form>
</body>
</html>
[/code]
succ.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value=" + title"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</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="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>

<package name="lee" extends="struts-default">

<action name="upload" class="lee.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
</action>
</package>
</struts>


UploadAction

// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: UploadAction.java

package lee;

import com.opensymphony.xwork2.ActionSupport;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

public class UploadAction extends ActionSupport
{

private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;

public UploadAction()
{
}

public void setSavePath(String value)
{
savePath = value;
}

private String getSavePath()
throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}

public void setTitle(String title)
{
this.title = title;
}

public void setUpload(File upload)
{
this.upload = upload;
}

public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}

public void setUploadFileName(String uploadFileName)
{
this.uploadFileName = uploadFileName;
}

public String getTitle()
{
return title;
}

public File getUpload()
{
return upload;
}

public String getUploadContentType()
{
return uploadContentType;
}

public String getUploadFileName()
{
return uploadFileName;
}

public String execute()
throws Exception
{
System.out.println("开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println((new StringBuilder()).append("==========").append(getUploadFileName()).toString());
System.out.println((new StringBuilder()).append("==========").append(getUploadContentType()).toString());
System.out.println((new StringBuilder()).append("==========").append(getUpload()).toString());
FileOutputStream fos = new FileOutputStream((new StringBuilder()).append(getSavePath()).append("\\").append(getUploadFileName()).toString());
FileInputStream fis = new FileInputStream(getUpload());
byte buffer[] = new byte[1024];
for (int len = 0; (len = fis.read(buffer)) > 0;)
fos.write(buffer, 0, len);

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.custom.i18n.resources" value="globalMessages"/>


使用解码集

<constant name="struts.i18n.encoding" value="GBK"/>

<package name="upload" extends="struts-default">

<action name="upload" class="lee.UploadAction">


拦截器

<interceptor-ref name="fileUpload">


允许上传文件
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param> 

上传文件大小
<param name="maximumSize">2000</param> 
</interceptor-ref>
<interceptor-ref name="defaultStack"/>


动态ACTION属性
<param name="savePath">/upload</param>


必须配置input视图
<result name="input"> /upload.jsp</result> 
<result>/succ.jsp</result>
</action>

</package>
</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值