求一个实现上传和下载文件的例子
最好下载的比较详细,小弟新手,感激不尽
邮箱:sun910601@sina.com
[解决办法]
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
?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.devMode" value="false" />
<!-- 改变struts2默认为2M的上传文件大小限制 -->
<constant name="struts.multipart.maxSize" value="10240000"/>
<package name="upload" extends="struts-default">
<action name="uploadFile" class="com.labci.struts2.action.UploadFileAction">
<param name="savePath">/upload</param>
<param name="allowTypes">text/plain,text/xml,text/html,image/gif,image/png,image/jpeg,image/jpg,image/bmp</param>
<result name="success">index.jsp</result>
<result name="input">index.jsp</result>
</action>
<action name="downloadFile" class="com.labci.struts2.action.DownloadFileAction">
<param name="savePath">/upload</param>
<result name="success">index.jsp</result>
</action>
</package>
</struts>
/**
* Struts2Test
* 文件下载的Action
*/
package com.labci.struts2.action;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Bill Tu(tujiyue/iwtxokhtd)
* Jun 8, 2011[9:15:15 PM]
*
*/
public class DownloadFileAction extends ActionSupport implements
ServletRequestAware, ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = -7448748577778248376L;
private HttpServletRequest request;
private HttpServletResponse response;
private String savePath;
@Override
public String execute() throws Exception {
String fileName=request.getParameter("fileName");
String fullPath=getSavePath()+"//"+fileName;
fileName=new String(fileName.getBytes("utf-8"),"iso-8859-1");
InputStream is=new FileInputStream(fullPath);
int len=0;
byte []buffers=new byte[1024];
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename=/""+fileName+"/"");
//把文件内容通过输出流打印到页面上供下载
while((len=is.read(buffers))!=-1){
OutputStream os=response.getOutputStream();
os.write(buffers, 0, len);
}
is.close();
return SUCCESS;
}
public void setServletRequest(HttpServletRequest req) {
this.request=req;
}
public void setServletResponse(HttpServletResponse resp) {
this.response=resp;
}
@SuppressWarnings("deprecation")
public String getSavePath() {
return request.getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}