/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.upload.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
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 com.upload.entity.FileUp;
import com.upload.form.FileActionForm;
import com.upload.service.FileUpService;
public class FileAction extends DispatchAction {
public ActionForward upLoadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Date date=new Date();
FileActionForm fileActionForm = (FileActionForm) form;
FileUp fileup = new FileUp();
FormFile file=fileActionForm.getFile(); //获取当前文件
String dir=servlet.getServletContext().getRealPath("/image");
//调用图片的上传的方法,并且返回上传服务器的路径
String path=upload(dir,file);
path="image/"+path;
fileup.setFilePath(path);
fileup.setFileName(fileActionForm.getFileName());
fileup.setFileSize(String.valueOf(fileActionForm.getFileSize()));
fileup.setFileDate(DateFormat.getDateInstance().format(date));
try{
FileUpService service = new FileUpService();
service.addFile(fileup);
List<FileUp> fileups = new ArrayList<FileUp>();
List list =service.getFileUp("FileUp");
if(list != null){
for(Object object : list){
FileUp f = (FileUp)object;
fileups.add(f);
}
}
request.setAttribute("addfileup", fileups);
return mapping.findForward("success");
}catch(Exception e){
e.printStackTrace();
return mapping.findForward("error");
}
}
/**
* 以下方法是实现图片文件上传的功能
*
* 文件上传函数
*/
public String upload(String dir,FormFile formFile)
throws Exception{
String fname=formFile.getFileName();
int i=fname.lastIndexOf(".");
String type= fname.substring(i);
fname=System.currentTimeMillis()+"."+type;
//创建读取用户上传文件的对象
InputStream streamIn=formFile.getInputStream();
//创建把上传数据写到目标文件的对象
File uploadFile=new File(dir);
if(!uploadFile.exists()||uploadFile==null){
uploadFile.mkdirs();
}
//判断指定路径是否存在,不存在则创建路径
String path=uploadFile.getPath()+"/"+fname;
OutputStream streamOut=new FileOutputStream(path);
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=streamIn.read(buffer,0,8192))!=-1){
streamOut.write(buffer,0,bytesRead);
}
streamOut.close();
streamIn.close();
formFile.destroy();
return fname;
}
/**
* 图片下载方法
*/
public ActionForward downFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception{
String path=request.getParameter("path");
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
OutputStream fos=null;
InputStream fis=null;
String filepath=servlet.getServletContext().getRealPath("/"+path);
File uploadFile =new File(filepath);
fis= new FileInputStream(uploadFile);
bis= new BufferedInputStream(fis);
fos=response.getOutputStream();
bos= new BufferedOutputStream(fos);
response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(path,"utf-8"));
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=bis.read(buffer,0,8192))!=-1){
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
return null;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Struts+hibernate图片上传
共33个文件
xml:7个
class:5个
java:5个
4星 · 超过85%的资源 需积分: 9 84 下载量 35 浏览量
2009-02-20
14:58:20
上传
评论 2
收藏 28KB RAR 举报
温馨提示
一个用Struts+hibernate实现的图片上传的程序,mysql数据库。表已经建好导入即可
资源推荐
资源详情
资源评论
收起资源包目录
uptest.rar (33个子文件)
uptest
etc
table.sql 156B
.project 1KB
.mymetadata 291B
WebRoot
WEB-INF
struts-tiles.tld 8KB
.struts-config.mex 483B
web.xml 1KB
struts-config.xml 808B
struts-bean.tld 9KB
struts-nested.tld 70KB
struts-html.tld 72KB
struts-logic.tld 14KB
validator-rules.xml 13KB
lib
classes
com
yourcompany
struts
ApplicationResources.properties 89B
upload
action
FileAction.class 6KB
mapping
FileUp.hbm.xml 731B
form
FileActionForm.class 2KB
service
FileUpService.class 1KB
entity
FileUp.class 1KB
util
FileUpUtil.class 2KB
hibernate.cfg.xml 903B
index.jsp 3KB
META-INF
MANIFEST.MF 39B
fileup.jsp 2KB
.myeclipse
src
com
yourcompany
struts
ApplicationResources.properties 89B
upload
action
FileAction.java 4KB
mapping
FileUp.hbm.xml 731B
form
FileActionForm.java 2KB
service
FileUpService.java 761B
entity
FileUp.java 868B
util
FileUpUtil.java 2KB
hibernate.cfg.xml 903B
.mystrutsdata 262B
.classpath 3KB
共 33 条
- 1
资源评论
- LUCK--NingNing2014-06-18内容很丰富,很好用用!
- 聆风杰2012-09-12比我想象的内容要丰富,不错,自己改动一下就可以了
- fengda28702011-11-08还可以 挺好的。可以运行,但是需要修改一点东西!
liudashao
- 粉丝: 17
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功