struts 上传文件

1,action类:

/**
*
*/
package org.jeelee.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
* @author JeeLee
*
*/
public class UploadFileAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16*1024;
private File myFile ;
private String contentType ;
private String fileName ;
private String caption ;
private String imageFileName ;
/**
* 用struts2标签<s:file/>实现上传文件要注意:
* <s:file name="xxx" />对应Action类里面的xxx、xxxContentType和xxxFileName三个属性
*/
public UploadFileAction() {
// TODO Auto-generated constructor stub
}

/**
* 这个方法很关键,要想实现上传文件,这个方法是必须的(上传文件的MIME类型),
* 它是由<s:file/>标签绑定的:set+file标签名+FileContentType
* @author JeeLee
* @param contentType
*/
public void setMyFileContentType(String contentType)
{
this.contentType = contentType ;
}
/**
* 这个方法很重要,要想实现上传文件,这个方法是必须的(上传文件的文件名,该文件名不包括文件的路径),
* 它是由<s:file/>标签绑定的:set+file标签名+FileName
* @author JeeLee
* @param fileName
*/
public void setMyFileFileName(String fileName)
{
this.fileName = fileName ;
}


public String getMyFileContentType()
{
return contentType ;
}

public String getMyFileFileName()
{
return fileName ;
}

public void setMyFile(File myFile)
{
this.myFile = myFile ;
}

public File getMyFile()
{
return myFile ;
}

public void setCaption(String caption)
{
this.caption = caption ;
}

public String getCaption()
{
return caption ;
}

public void setImageFileName(String imageFileName)
{
this.imageFileName = imageFileName ;
}

public String getImageFileName()
{
return imageFileName ;
}

/**
* 复制文件
* @author JeeLee
* @param srcFile
* @param desFile
*/
public void copyFile(File srcFile,File desFile)
{

InputStream in = null ;
OutputStream out = null ;

try
{
in = new BufferedInputStream(new FileInputStream(srcFile),BUFFER_SIZE) ;
out = new BufferedOutputStream(new FileOutputStream(desFile),BUFFER_SIZE) ;

byte[] buffer = new byte[BUFFER_SIZE];
while(in.read(buffer)>0)
{
out.write(buffer) ;
}
}
catch(IOException e)
{
System.out.println("读取文件错误:"+e);
}
finally
{
try
{
if(in!=null)
{
in.close();
}
if(out!=null){
out.close();
}
}
catch(IOException e)
{
System.out.println("关闭文件失败:"+e);
}
}
}

/**
* 获得文件扩展名
* @author JeeLee
* @param fileName
* @return
*/
public String getExtention(String fileName)
{
System.out.println("file name ="+fileName);
int pos = fileName.lastIndexOf(".");

return fileName.substring(pos) ;
}

/**
* 上传文件的action
* @author JeeLee
* @return
*/
public String uploadFile()
{
String imgFile;
System.out.println("test------------------");
imageFileName = new Date().getTime() + getExtention(fileName);
imgFile = ServletActionContext.getServletContext().getRealPath("/pic")+"/"+imageFileName ;
System.out.println("imgFile=="+imgFile);
File imageFile = new File(imgFile);

copyFile(myFile,imageFile) ;

return "UPLOAD:SUCCESS" ;
}

public String deleteFile()
{
myFile.delete();

return "DELETE:SUCCESS" ;
}

/*@Override
public String execute()
{
imageFileName = new Date().getTime() + getExtention(fileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/pic")+"/"+imageFileName);

copyFile(file,imageFile) ;

return SUCCESS ;

}*/

}


2,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" />
<package name="jeelee" extends="struts-default">
<!--上传文件-->
<action name="uploadFile" class="org.jeelee.action.UploadFileAction">
<result name="UPLOAD:SUCCESS">showUploadFile.jsp</result>
<result name="DELETE:SUCCESS">showUploadFile.jsp</result>
</action>
</package>
</struts>


3,JSP文件:
上传页面:loadfile.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=UTF-8">
<title>struts2 上传文件</title>
</head>
<body>
<s:form action="uploadFile.action" enctype ="multipart/form-data" >
<s:file name="myFile" label="上传文件"></s:file>
<s:textfield name="fileName" label="caption"></s:textfield>
<s:submit name="ccc" method="uploadFile" value="上传"></s:submit>
<s:submit name="ddd" method="deleteFile" value="删除"></s:submit>
</s:form>
</body>
</html>

显示结果页面:

<%@ 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=UTF-8">
<title>struts2 上传文件结果显示</title>
</head>
<body>
<div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
<img src ='<s:property value ="imageFileName" />' />
<br />
<s:property value ="caption" />
</div >

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值