以下是接收参数的actionForm
package com.cs.formbean;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class FileUpActionForm extends ActionForm {
private String title ;
private FormFile upfile ;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public FormFile getUpfile() {
return upfile;
}
public void setUpfile(FormFile upfile) {
this.upfile = upfile;
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
//可以在此判断文件的后缀名,用来阻挡不合格的上传文件
return super.validate(mapping, request);
}
}
fileup.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="fileup.do" method="post" enctype="multipart/form-data" >
标题:<input type="text" name="title" /><br>
文件:<input type="file" name="upfile" /><br>
<input type="submit" value="上传" >
</form>
</body>
</html>
action中的上传
private Share saveUpFile(FileUpActionForm taform ) {
//upfile封装在了FileUpActionForm 中的upfile属性
//定义流对象,这里用的是缓冲流
FileOutputStream fos = null ;
InputStream is = null ;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//获取upfile对象
FormFile formFile = taform.getUpfile() ;
//建立文件流
File newFile = new File ("d:\\share\\"+formFile.getFileName()) ;
fos = new FileOutputStream(newFile) ;
is = formFile.getInputStream() ;
bis = new BufferedInputStream(is) ;
bos = new BufferedOutputStream(fos) ;
//缓冲区定义为4K
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
throw new SystemException("文件目录不存在");
} catch (IOException e) {
throw new SystemException("上传失败!");
}finally{
try {
bos.flush();
fos.close();
is.close();
bis.close();
bos.close();
} catch (IOException e) {
throw new SystemException("关闭IO流遇到异常") ;
}
}
shareMgr.addFile(share) ;
return share ;
}
下载:
假定存在资源文件d:\\share目录
在struts的Action中的下载方法
前提,上传的时候,在数据库中存的只是资源文件的路径,所以从数据库中读取到的是路径
而非真正的文件
public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
/*
* 下载需要三个参数
* 1.path:建立文件流的时候获取路径
* 2.name:下载到本地想自己改个名字,所以要获取本地名
* 3.size:可要可不要
*/
//建立流对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
FileUpActionForm taform = (FileUpActionForm)form ;
//这里存在中文乱码的问题
//所以要先读取路径的前半部分
String path = taform.getPath().substring(0, taform.getPath().lastIndexOf("\\")) ;
//下载的时候,下载到本地的名称由用户输入,且封装在了
//FileUpActionForm 中的name中
try {
//转码
path = path +"\\"+ new String(taform.getName().getBytes("iso8859-1"),"gbk") ;
} catch (UnsupportedEncodingException e1) {
throw new SystemException("不支持的编码转换") ;
}
System.out.println("path = "+path);
File downfile = new File(path) ;
if(!downfile.exists()){
throw new SystemException("文件不存在") ;
}
int size = (int)taform.getSize() ;
//如果是从服务器上取就用这个获得系统的绝对路径方法。
try {
fis = new FileInputStream(downfile) ;
bis = new BufferedInputStream(fis);
//首先要拿到这个流对象 ,然后才能写到本地去
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
//这个就就是弹出下载对话框的关键代码
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition",
"attachment;filename=" +
URLEncoder.encode(path, "utf-8"));
response.setContentLength(taform.getSize());
int bytesRead = 0;
//用输入流进行先读,然后用输出流去写(缓冲输入输出流)
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
throw new SystemException("文件没找到") ;
} catch (UnsupportedEncodingException e) {
throw new SystemException("不支持的编码转换") ;
} catch (IOException e) {
throw new SystemException("I/O异常") ;
} finally{
try {
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (IOException e) {
throw new SystemException("关闭IO流遇到异常") ;
}
}
return mapping.findForward("share") ;
}