[size=large]1.上传
分三步:(1)导入相应的jar包commons-fileupload-1.2.1.jar,commons-io-1.2.1.jar。
(2)设置form表的enctype="multipart/form-data"属性
(3)在action中添加以下属性:
private File image;//文件
private String imageFileName;//文件名[/size]
实例:jsp文件:
struts.xml配置文件:
action类:
至此基本完成上传功能
2.下载
将文件名从数据库取出来并迭代在JSP页面:
strut2.xml文件配置:
action类:
至此,下载功能基本完成
分三步:(1)导入相应的jar包commons-fileupload-1.2.1.jar,commons-io-1.2.1.jar。
(2)设置form表的enctype="multipart/form-data"属性
(3)在action中添加以下属性:
private File image;//文件
private String imageFileName;//文件名[/size]
实例:jsp文件:
<body style="background-image: url(../css/img/background.png);" >
<form action="upload.action" namespace="/admin" enctype="multipart/form-data" method="post" theme="simple">
文件:<input type="file" name="image"/>
<input type="submit" value="上传"/>
</form>
</body>
struts.xml配置文件:
<action name="upload" class="uploadAction" >
<result name="success">/admin/message.jsp</result>
</action>
action类:
import com.opensymphony.xwork2.ActionContext;
public class UploadAction {
private File image;//和JSP的name属性一致,类型为File
private String imageFileName;//文件名,必须是name属性+FileName
private UploadService uploadService;
private UploadFile uploadFile;//自定义一个实体类保存上传文件
public String execute() throws Exception{
String realPath = ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println("保存地址:"+realPath);
if(image != null){
File saveFile = new File(new File(realPath),imageFileName);
System.out.println("文件名:"+imageFileName);
if(!saveFile.getParentFile().exists()) saveFile.getParentFile().mkdirs();
FileUtils.copyFile(image, saveFile);
uploadFile = new UploadFile();
uploadFile.setfPath(realPath);
uploadFile.setfName(imageFileName);
uploadService.saveFile(uploadFile);//保存至数据库
ActionContext.getContext().put("message", "上传成功!");
}
return "success";
}
//getter/setter....
}
至此基本完成上传功能
2.下载
将文件名从数据库取出来并迭代在JSP页面:
<s:iterator value="list" var="l">
<tr>
<td>${l.fName}</td>
<td><a href="../employee/down.action?fileName=${l.fName}" >下载</a></td>
</tr>
</s:iterator>
strut2.xml文件配置:
<!--下载文件 -->
<action name="down" class="downFileAction" >
<result name="success" type="stream">
<!-- 指定下载文件的内容类型,text/plain是默认类型 -->
<param name="contentType">${contentType}</param>
<!-- inputName默认值是inputStream,如果action中用于读取下载文件内容的属性名是inputStream,那么可以省略这个参数 -->
<param name="inputName">inputStream</param>
<!--动态获取文件名,从Action中的取得filename-->
<param name="contentDisposition">
attachment;fileName="${fileName}"
</param>
<param name="bufferSize">4096</param>
</result>
</action>
action类:
public class DownFileAction extends BaseAction{
private String fName;
private String contentType;
private String inputPath;
private String fileName;
public InputStream getInputStream() throws UnsupportedEncodingException{
byte[] cc = fileName.getBytes("iso-8859-1");
fName = new String(cc,"utf-8");
System.out.println("请求地址:"+"/upload/"+fName);
return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fName);
}
public String execute(){
System.out.println("fileName:"+fileName);
return "success";
}
//getters和setters
}
至此,下载功能基本完成