你好,游客 登录 注册 搜索
背景:
阅读新闻

struts2文件上传(保存为BLOB格式)

[日期:2014-06-09] 来源:Linux社区  作者:monsoo [字体: ]

struts2文件上传(保存为BLOB格式)

Struts2的入门实例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2实现ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm

遇到的Struts2文件下载乱码问题 http://www.linuxidc.com/Linux/2014-03/98990.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

Struts2 注解模式的几个知识点 http://www.linuxidc.com/Linux/2013-06/85830.htm

html文件:提供上传文件的入口
<input type="file" name="upload"><!-- name很重要,与后面action文件对应 -->
xml文件:
<!-- 上传资料 -->
<action name="upload" class="web.FileUploadAction" method="execute">
        <!-- 上传成功,返回 -->
 <result name="success">success_commit.jsp</result>
 <result name="error">error.jsp</result>
</action>
action文件:

public class FileUploadAction {
 private String remark;// 备注
 private int id;// id
 private File upload;// 与html文件中input[type=file]必须一样
 private String uploadFileName;// 文件名,必须这样写,upload和html文件中input[type=file]必须,FileName
 
 ResultService rService = new ResultService();

 public String execute() {
  try {
   // 点击上传之后
   // 保存上传数据
   int row = 0;
   // 检查是否上传了文件
   if (uploadFileName != null && !uploadFileName.equals("")) {
          //考虑到需要保存文件进行的操作比较多,上传了文件另外保存
    row = rService.save(upload, uploadFileName, demandid,
      remark);

   } else {
          // 没有上传文件
    row = rService.save(demandid, remark);
   }

   if (row == 1) {// 修改的行数
          // 上传成功
    return "success";
   } else {
          //上传失败
    return "error";
   }
  } catch (Exception e) {
   return "error";
  }
 }
 
 //  以下是getter/setter方法(略去)
}
service层

 public int save(File file, String filename, int id, String remark) {
 // 有文件保存方法
  String lj = copyfile(file, filename);
  Result res = new Result();
  res.setFile(new File(lj));
  res.setDemandid(id);
  res.setRemark(remark);
  res.setTitle(filename);
  return rDao.saveResult(res);
 }
 //没有文件的保存方法
 public int save(int id,String remark){
  Result res = new Result();
  res.setDemandid(id);
  res.setRemark(remark);
  return rDao.saveResultNoFile(res);
 }
 
 public String copyfile(File file, String filename) {
        // 把文件拷贝到服务器的指定目录下
    String realpath = ServletActionContext.getServletContext().getRealPath(
   "/info");
    String lj = realpath + "\\" + filename;
    try {
  if (file != null) {
   File savefile = new File(new File(realpath), filename);
   if (!savefile.getParentFile().exists()) {
    savefile.getParentFile().mkdirs();
   }
   // 调用copyFile方法
   FileUtils.copyFile(file, savefile);
  }
    } catch (Exception e) {
     e.printStackTrace();
    }
    return lj;
        }

dao层(与数据库进行交互)
    public int saveResult(Result res) {// 保存有文件的
 int row = 0;
 Connection conn = null;
 PreparedStatement ps = null;
 String sql = "insert into result(id,content,remark,time,title,demandid) values(id_result.nextval,?,?,?,?,?)";// Oracle数据库
 // id_result 序列号
 try {
  conn = DBUtil.getConn();
  File file = res.getFile();
  FileInputStream fis = new FileInputStream(file);
  ps = conn.prepareStatement(sql);
  int len = (int)file.length();
  ps.setBinaryStream(1, fis, len);
  ps.setString(2, res.getRemark());
  ps.setLong(3, System.currentTimeMillis());
  ps.setString(4, res.getTitle());
  ps.setInt(5, res.getDemandid());

  row = ps.executeUpdate();
  fis.close();

 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  DBUtil.close(conn, ps, null);
 }
 // 返回修改的行数
 return row;
}

OK.本人亲测

时间关系,没有来得及注释的,下次补上。

Struts 的详细介绍请点这里
Struts 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2014-06/102905.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款