SSH框架开发通过common-fileupload组件实现上传下载

smallrookie 2010-01-14 03:20:56
请大纳们给讲解用SSH框架开发通过common-fileupload来实现上传下载,谢谢了,如果没有时间,有没有现成的代码了,我自己看就行了,谢谢了!!
...全文
1238 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
archko 2010-01-16
  • 打赏
  • 举报
回复
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"
smallrookie 2010-01-15
  • 打赏
  • 举报
回复
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);
}

}
qianzhimeiying 2010-01-14
  • 打赏
  • 举报
回复
strut2,spring2.5
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

Z_FEI 2010-01-14
  • 打赏
  • 举报
回复
给你个网站自己找吧!www.pudn.com
Struts 2需要的jar包: 1.commons-fileupload.jar(commons项目中的关于文件上传的包, struts2.1.6版本后必须加入此文件) 2.commons-io.jar(commons项目(commons项目就是java中一些常用的公共的组件)的io子项目,是处理异常的) 3.freemarker-2.3.15.jar(支持freemarker的,在webwork中也有) 4.javassist.jar(一个开源的分析、编辑和创建Java字节码的类库,hibernate中也需要,引入其中一个即可) 5.ognl-2.7.3.jar(支持ognl语言) 6.struts2-core-2.1.8.jar(struts2的核心jar包) 7.xwork-core-2.1.6.jar(xwork的核心jar包) 8.servlet.jar 9.commons-lang-2.5.jar(commons项目中的lang包,一般不需要,不用也不出错) 10.struts2-spring-plugin-2.1.8.jar(struts2与spring集成时使用的) Spring需要的jar包: 1.spring.jar(里面含有spring的所有核心类库) 2.commons-logging-1.1.1.jar(ASF出品的日志包,struts2 2、spring、hibernate框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录) 3.common-annotations.jar(支持注解的包) 4.aspectjrt.jar(支持AOP的包) 5.aspectjweaver.jar(支持AOP的包) 6.cglib-nodep-2.1_3.jar(支持cglib动态代理的包) 如果用BasicDataSource来配置数据库连接,还要加入2个包: 7.commons-pool.jar 8.commons-dbcp.jar Hibernate需要的jar包: 1.hibernate3.jar(hibernate的核心jar包) 2.antlr-2.7.2.jar(语言转换工具,hibernate利用它实现HQL到SQL的转换) 3.commons-collections-3.2.1.jar(commons项目中的子项目,是对collection集合的封装) 4.dom4j-1.6.1.jar(对dom4j的封装,是解析xml文件的) 5.javassist-3.9.0.GA.jar(一个开源的分析、编辑和创建Java字节码的类库) 6.jta-1.1.jar(hibernate对事务的处理) 7.slf4j-api-1.6.4.jar(一个日志系统的服务的api) 8.slf4j-nop-1.6.4.jar(对slf4j-api-x.x.x.jar的一个实现) 9.ojdbc14.jar (oracle驱动) 10.mysql-connector-java-5.1.6-bin.jar (mySql驱动) 如果使用注解还需添加hibernate-annotations-3.4.0.GA包: 11.hibernate-annotations.jar 12.ejb3-persistence.jar 13.hibernate-commons-annotations.jar

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧