实现简单的struts,hibernate上传文件

别的不多说直接开整,没有注释和配置文件,不过程序是通的

上传页面 upload.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head></head>

 <body>
  <form action="upLoad..do method="post" enctype="multipart/form-data"
   name="form1">
   <p>
    上传到服务器并保存到数据库!
    <input name="theFile" type="file" id="theFile">
   </p>
   <p>
    <select name="type" value="">
     <option>choose</option>
     <option>1</option>
     <option>2</option>
     <option>3</option>
    </select>
   </p>
   <p>
    <input type="submit" name="Submit" value="提交">
   </p>
  </form>
 </body>
</html> 

转发后的jsp页 saveOk.jsp

<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
strutsUploadResult
</title>
</head>
<body bgcolor="#ffffff">
<h1>
上传结果
</h1>
文件名:<%=request.getAttribute("fileName")%><br />
模板类型:<%=request.getAttribute("type")%><br />
文件类型:<%=request.getAttribute("fileType")%><br />
文件大小:<%=request.getAttribute("fileSize")%><br />
</body>
</html>

ActionForm  UpLoadForm .java

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class UpLoadForm extends ActionForm {
 private String type;

 private org.apache.struts.upload.FormFile theFile;
 public org.apache.struts.upload.FormFile getTheFile() {
  return theFile;
 }

 public void setTheFile(org.apache.struts.upload.FormFile theFile) {
  this.theFile = theFile;
 }

 public ActionErrors validate(ActionMapping actionMapping,
   HttpServletRequest httpServletRequest) {

 // if (!this.getTheFile().getContentType().equals("image/pjpeg")) {
//  }
  //if (this.getTheFile().getFileSize() > 100) {
  //}
//  return null;
 }

 public void reset(ActionMapping actionMapping,
   HttpServletRequest httpServletRequest) {
  theFile = null;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }
}

Action UpLoadAction.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.caexpo.bean.Path;
import com.caexpo.dao.SaveTemplate;
import com.caexpo.model.WcmTemplate;
import com.caexpo.struts.form.UpLoadForm;
public class UpLoadAction extends Action {
 /*
  * Generated Methods
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  UpLoadForm upLoadForm = (UpLoadForm) form;
  // TODO Auto-generated method stub
  WcmTemplate wt = new WcmTemplate();
  SaveTemplate st = new SaveTemplate();
  FormFile file = upLoadForm.getTheFile()
  Path filePath = Path.getInstance();
  String path = filePath.getPath() + "/upload/" + file.getFileName();
  try {
   InputStream input = file.getInputStream();
   OutputStream output = new FileOutputStream(path);
   int bytesReader = 0;
   byte[] readbuffer = new byte[8192];
   while ((bytesReader = input.read(readbuffer, 0, 8192)) != -1) {
    output.write(readbuffer, 0, bytesReader);
   }
   output.close();
   // File read = new File(file.getFileName());
   FileInputStream fileinputstream = new FileInputStream(path);
   int lenght = fileinputstream.available();
   byte bytes[] = new byte[lenght];
   fileinputstream.read(bytes);
   fileinputstream.close();
   String con = new String(bytes);
   wt.setContent(con);
   wt.setTemplatename(file.getFileName());
   wt.setType(upLoadForm.getType());
   st.SaveTemplate(wt);
  } catch (Exception ex) {
   System.out.println(ex.getMessage());
  }
  request.setAttribute("type", upLoadForm.getType());
  request.setAttribute("fileName", file.getFileName());
  request.setAttribute("fileSize", new Integer(file.getFileSize()));
  request.setAttribute("fileType", file.getContentType());
  return mapping.findForward("success");
 }
}

DAO   SaveTemplate .java

import org.hibernate.Session;
import org.hibernate.Transaction;
import com.caexpo.hibernate.HibernateSessionFactory;

public class SaveTemplate {

 public void SaveTemplate(WcmTemplate wt) {
  Session session = HibernateSessionFactory.getSession();
  Transaction tr = session.beginTransaction();
  try {
   session.save(wt);
   tr.commit();
  } catch (Exception ex) {
   tr.rollback();
   System.out.println(ex.getMessage());
  } finally {
   session.close();
  }
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值