struts2上传文件
来源:互联网 发布:破解软件盒子 编辑:程序博客网 时间:2024/05/28 23:21
Struts2上传文件
1.上传页面: Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
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>
</head>
<body>
<s:form action="upload.action" method="POST" enctype="multipart/form-data">
<s:file label="选择文件" name="upFile"></s:file>
<s:submit label="上传" />
</s:form>
</body>
</html>
2.上传成功页面(上传成功并显示图片): upSuc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
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>
<title>ok</title>
</head>
<body>
上传<br/>
<img src="<%=basePath%><s:property value="savePath + '/' + upFileFileName"/>"/>
</body>
</html>
3.ACTION类: UploadAction.java
package cn.test.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private File upFile;
private String upFileFileName; //上传的文件名 (1.系统自动注入 2.变量命名有规则: 前台对象名+"FileName")
private String upFileContentType; //文件类型 (1.系统自动注入 2.变量命名有规则: 前台对象名+"ContentType")
private String savePath;
@Override
public String execute() throws Exception {
String path = ServletActionContext.getServletContext().getRealPath(getSavePath()) + "/" + upFileFileName;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(upFile));
bos = new BufferedOutputStream(new FileOutputStream(path));
byte[] buf = new byte[(int)upFile.length()];
int len = 0;
while(((len=bis.read(buf))!=-1)){
bos.write(buf, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
}catch(Exception e){
bos = null;
bis = null;
}
}
return SUCCESS;
}
public File getUpFile() {
return upFile;
}
public void setUpFile(File upFile) {
this.upFile = upFile;
}
public String getUpFileFileName() {
return upFileFileName;
}
public void setUpFileFileName(String upFileFileName) {
this.upFileFileName = upFileFileName;
}
public String getUpFileContentType() {
return upFileContentType;
}
public void setUpFileContentType(String upFileContentType) {
this.upFileContentType = upFileContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
4.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 name="upload" class="cn.test.action.UploadAction">
<param name="savePath">/pic</param>
<result name="success">/upSuc.jsp</result>
<result name="input">/index.jsp</result>
<interceptor-ref name ="fileUpload">
<param name ="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
</interceptor-ref>
<interceptor-ref name ="defaultStack"/>
</action>
</package>
</struts>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- 类似Portal的浮动窗口,其实也就是类似QQ空间的
- CArchive原理(转)
- FCKeditor(Asp.Net)的配置与安全性
- 写出高性能SQL语句的35条方法
- 第一次
- struts2上传文件
- Snake Game
- 做好一件事情
- XML文件学习
- 《EJB 3 in Action》读书笔记02
- 我们最缺的是投资机会
- 有趣的编程题和面试题
- 每个项目中,你不得不知的11个Java第三方类库
- VPN隧道协议发展现状