首页 / 操作系统 / Linux / struts2文件上传(保存为BLOB格式)
struts2文件上传(保存为BLOB格式) Struts2的入门实例 http://www.linuxidc.com/Linux/2013-05/84618.htmStruts2实现ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm遇到的Struts2文件下载乱码问题 http://www.linuxidc.com/Linux/2014-03/98990.htmStruts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htmStruts2 注解模式的几个知识点 http://www.linuxidc.com/Linux/2013-06/85830.htmhtml文件:提供上传文件的入口 <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
收藏该网址