SSH框架开发通过common-fileupload组件实现上传下载 请大纳们给讲解用SSH框架开发通过common-fileupload来实现上传下载,谢谢了,如果没有时间,有没有现成的代码了,我自己看就行了,谢谢了!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你个网站自己找吧!www.pudn.com strut2,spring2.5jsp端<input type="file" name="txtFile" />form里属性enctype="multipart/form-data"action extends ActionSupport,里面设置2个属性,上面的input的name是txtFile,则此input必须在action里声明两个变量txtFile类型File,txtFileFileName类型String,为这两个变量添加get,set方法然后即可在action的execute方法中File newFile = new File("XXX");上传来的文件名是一长串+tmp,因此file控件自己会另外传个FileName上来,就是那个txtFileFileName//copy from txtFile to newFile package updown.action;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URLEncoder;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DispatchAction;import org.apache.struts.upload.FormFile;import updown.domain.UpDown;import updown.form.UploadForm;import updown.service.UpDownService;public class UpDownAction extends DispatchAction { private UpDownService upDownService; public void setUpDownService(UpDownService upDownService) { this.upDownService = upDownService; } // 显示下载页面 public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { List<UpDown> upDownList = upDownService.getAllUpDown(); request.setAttribute("upDownList", upDownList); return mapping.findForward("success"); } // 转到上传页面 public ActionForward toUpload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("success"); } // 上传文件 public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UploadForm uploadForm = (UploadForm) form; FormFile file = uploadForm.getMyFile(); FileOutputStream fos = null; String realPath = null; try { String name = file.getFileName(); byte[] fileData = file.getFileData(); // 获得upfile目录的绝对路径 // ServletContext sc = this.getServlet().getServletContext(); // String realPath = sc.getRealPath("/xxx/");//webapp路径 realPath = new File("\\.").getCanonicalFile().toString();// 得到当前磁盘路径 fos = new FileOutputStream(realPath + "/" + name); fos.write(fileData); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } UpDown upDown = new UpDown(); upDown.setName(file.getFileName()); upDown.setPath(realPath); upDownService.saveUpDown(upDown); return mapping.findForward("success"); } // 下载文件 public ActionForward download(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Integer id = new Integer(request.getParameter("id"));// 根据id下载文件 UpDown upDown = upDownService.getUpDown(id); String fileName = upDown.getName(); File file = new File(upDown.getPath() + "/" + fileName);// 下载路径 InputStream is = new FileInputStream(file); OutputStream os = response.getOutputStream(); BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(os); response.reset(); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-msdownload");// 下载属性 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(upDown.getName(), "utf-8"));// 下载的文件名转换成utf-8 int bytesRead = 0; byte[] buffer = new byte[1024 * 8]; while ((bytesRead = bis.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } bos.flush(); bis.close(); bos.close(); is.close(); os.close(); return null; }}package updown.dao;import java.util.List;import updown.domain.UpDown;public interface UpDownDao { public UpDown findById(Integer id); public void save(UpDown upDown); public List<UpDown> findAll();}package updown.dao.impl;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.orm.hibernate3.HibernateTemplate;import updown.dao.UpDownDao;import updown.domain.UpDown;import updown.service.UpDownService;public class UpDownDaoImpl implements UpDownDao { protected HibernateTemplate hibernate; public void setHibernate(HibernateTemplate hibernate) { this.hibernate = hibernate; } public UpDown findById(Integer id) { String hql = "from UpDown ud where ud.id=?"; List<UpDown> list = hibernate.find(hql, new Object[] { id }); if (list == null || list.size() == 0) return null; return list.get(0); } public List<UpDown> findAll() { String hql = "from UpDown"; List<UpDown> list = hibernate.find(hql); return list; } public void save(UpDown upDown) { hibernate.save(upDown); }}package updown.domain;import java.io.Serializable;public class UpDown implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String name; private String path; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; }}package updown.form;import org.apache.struts.action.ActionForm;import org.apache.struts.upload.FormFile;public class UploadForm extends ActionForm { private static final long serialVersionUID = 1L; private FormFile myFile;//myFile必须与jsp中的一样 public FormFile getMyFile() { return myFile; } public void setMyFile(FormFile myFile) { this.myFile = myFile; }}package updown.service;import java.util.List;import updown.domain.UpDown;public interface UpDownService { public void saveUpDown(UpDown upDown); public List<UpDown> getAllUpDown(); public UpDown getUpDown(Integer id);}package updown.service.impl;import java.util.List;import updown.dao.UpDownDao;import updown.domain.UpDown;import updown.service.UpDownService;public class UpDownServiceImpl implements UpDownService { private UpDownDao upDownDao; public void setUpDownDao(UpDownDao upDownDao) { this.upDownDao = upDownDao; } public List<UpDown> getAllUpDown() { return upDownDao.findAll(); } public UpDown getUpDown(Integer id) { return upDownDao.findById(id); } public void saveUpDown(UpDown upDown) { upDownDao.save(upDown); }} Hashtable uploadFiles=ef.getMultipartRequestHandler().getFileElements(); if(uploadFiles.size()>0){ Enumeration fk=uploadFiles.keys(); while(fk.hasMoreElements()){ String fname=(String)fk.nextElement(); FormFile ff=(FormFile)uploadFiles.get(fname); //接下来知道了,不过每个 <input type=file name要不一样 >这个如果是动态添加的话用JS容易,否则你固定上传几个就定义几个,上传过程要判断下文件大小,因为有的文件选择框,没有选中文件会报错。ff.getInputStream输入流,其它不用说了吧。 } }这是Struts1的上传,当然表单 要设置enctype="multipart/form-data" 想参加个业余的java团队 JAVA Taglib的class文件是不是一定要定义到一个包中? struts:关于<html:checkbox>传值的问题 jsp问题,送100分,在线急等 跪求大神,求解DWR注册验证用户,修改错误 我编的jsp论坛,大家没事提题意见 分布式数据库 为什么IE弹出式窗口(JSP)里执行了两次? 这是什么地方错了? EL表达式 SSH整合看看错误指点一下哪些包不支持
jsp端
<input type="file" name="txtFile" />form里属性enctype="multipart/form-data"action extends ActionSupport,里面设置2个属性,上面的input的name是txtFile,
则此input必须在action里声明两个变量txtFile类型File,txtFileFileName类型String,
为这两个变量添加get,set方法然后即可在action的execute方法中
File newFile = new File("XXX");
上传来的文件名是一长串+tmp,因此file控件自己会另外传个FileName上来,就是那个txtFileFileName
//copy from txtFile to newFile
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;import updown.domain.UpDown;
import updown.form.UploadForm;
import updown.service.UpDownService;public class UpDownAction extends DispatchAction {
private UpDownService upDownService; public void setUpDownService(UpDownService upDownService) {
this.upDownService = upDownService;
} // 显示下载页面
public ActionForward show(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<UpDown> upDownList = upDownService.getAllUpDown();
request.setAttribute("upDownList", upDownList);
return mapping.findForward("success");
} // 转到上传页面
public ActionForward toUpload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("success"); } // 上传文件
public ActionForward upload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { UploadForm uploadForm = (UploadForm) form;
FormFile file = uploadForm.getMyFile();
FileOutputStream fos = null;
String realPath = null;
try {
String name = file.getFileName();
byte[] fileData = file.getFileData(); // 获得upfile目录的绝对路径
// ServletContext sc = this.getServlet().getServletContext();
// String realPath = sc.getRealPath("/xxx/");//webapp路径 realPath = new File("\\.").getCanonicalFile().toString();// 得到当前磁盘路径 fos = new FileOutputStream(realPath + "/" + name);
fos.write(fileData);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} UpDown upDown = new UpDown();
upDown.setName(file.getFileName());
upDown.setPath(realPath);
upDownService.saveUpDown(upDown); return mapping.findForward("success");
} // 下载文件
public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Integer id = new Integer(request.getParameter("id"));// 根据id下载文件
UpDown upDown = upDownService.getUpDown(id); String fileName = upDown.getName(); File file = new File(upDown.getPath() + "/" + fileName);// 下载路径 InputStream is = new FileInputStream(file);
OutputStream os = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os); response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");// 下载属性
response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(upDown.getName(), "utf-8"));// 下载的文件名转换成utf-8 int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
} bos.flush();
bis.close();
bos.close();
is.close();
os.close(); return null;
}}package updown.dao;import java.util.List;import updown.domain.UpDown;
public interface UpDownDao {
public UpDown findById(Integer id); public void save(UpDown upDown); public List<UpDown> findAll();}
package updown.dao.impl;import java.util.List;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;import updown.dao.UpDownDao;
import updown.domain.UpDown;
import updown.service.UpDownService;public class UpDownDaoImpl implements UpDownDao {
protected HibernateTemplate hibernate; public void setHibernate(HibernateTemplate hibernate) {
this.hibernate = hibernate;
} public UpDown findById(Integer id) {
String hql = "from UpDown ud where ud.id=?";
List<UpDown> list = hibernate.find(hql, new Object[] { id });
if (list == null || list.size() == 0)
return null;
return list.get(0);
} public List<UpDown> findAll() {
String hql = "from UpDown";
List<UpDown> list = hibernate.find(hql);
return list;
} public void save(UpDown upDown) {
hibernate.save(upDown);
}
}
package updown.domain;import java.io.Serializable;public class UpDown implements Serializable {
private static final long serialVersionUID = 1L; private Integer id;
private String name;
private String path; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPath() {
return path;
} public void setPath(String path) {
this.path = path;
}}
package updown.form;import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;public class UploadForm extends ActionForm {
private static final long serialVersionUID = 1L; private FormFile myFile;//myFile必须与jsp中的一样 public FormFile getMyFile() {
return myFile;
} public void setMyFile(FormFile myFile) {
this.myFile = myFile;
}}package updown.service;import java.util.List;import updown.domain.UpDown;public interface UpDownService {
public void saveUpDown(UpDown upDown); public List<UpDown> getAllUpDown(); public UpDown getUpDown(Integer id);}
package updown.service.impl;import java.util.List;import updown.dao.UpDownDao;
import updown.domain.UpDown;
import updown.service.UpDownService;public class UpDownServiceImpl implements UpDownService {
private UpDownDao upDownDao; public void setUpDownDao(UpDownDao upDownDao) {
this.upDownDao = upDownDao;
} public List<UpDown> getAllUpDown() {
return upDownDao.findAll();
} public UpDown getUpDown(Integer id) {
return upDownDao.findById(id);
} public void saveUpDown(UpDown upDown) {
upDownDao.save(upDown);
}}
Enumeration fk=uploadFiles.keys();
while(fk.hasMoreElements()){
String fname=(String)fk.nextElement();
FormFile ff=(FormFile)uploadFiles.get(fname);
//接下来知道了,不过每个 <input type=file name要不一样 >这个如果是动态添加的话用JS容易,否则你固定上传几个就定义几个,上传过程要判断下文件大小,因为有的文件选择框,没有选中文件会报错。
ff.getInputStream输入流,其它不用说了吧。
}
}
这是Struts1的上传,当然表单 要设置enctype="multipart/form-data"