首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

下传上载总结

2012-10-14 
上传下载总结?这里的上传是上传到数据库的blob字段中。前台的代码省略,只考虑上传下载关键代码。注意的一点

上传下载总结

?

这里的上传是上传到数据库的blob字段中。前台的代码省略,只考虑上传下载关键代码。

注意的一点是,上传必须把整个form提交上去,且这个form必须是:enctype="multipart/form-data";不能通过ajax方式提交,在action端只能通过form.getxxx()方式来得到值,不能通过request.getParameter()方式得到;同时,必须在servelt端不能用DispatchAction的方式,只能用继承Action的方式来得到值。

在action端的主要代码如下:

public class UpLoadAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {param_bean param = new param_bean();MultipartForm multipartForm = (MultipartForm) form;HttpRequestMgrComAction hrmc = new HttpRequestMgrComAction(request,response);try{parent_id = multipartForm.getPARENT_ID();//这里从actionform中去取值,而不是用request.getParameter()来取值if(!hrmc.isParamStrValid(parent_id)){parent_id = UtilComAction.createUUID();}MultipartRequestHandler multipartRequestHandler = form.getMultipartRequestHandler();Collection collection = multipartRequestHandler.getFileElements().values();Iterator iterator = collection.iterator();while (iterator.hasNext()) {FormFile formFile = (FormFile) iterator.next();if (formFile.getFileSize() > 0) {String filename = formFile.getFileName();int pnt = filename.lastIndexOf(".");if(pnt>-1){multipart_type = filename.substring(pnt);multipart_name = filename.substring(0,pnt);}else {multipart_type = "";multipart_name = filename;}param.add_param("ID", UtilComAction.createUUID());param.add_param("PARENT_ID", parent_id);param.add_param("MULTIPART_OBJ", formFile.getInputStream());param.add_param("MULTIPART_TYPE", multipart_type);param.add_param("MULTIPART_NAME", multipart_name);param = get_ejb.getMethod("Multipartmgr", "uploadFile", param);if(param.ret_code==0){}else{System.out.println(param.err_msg);throw new Exception(param.err_msg);}}}}catch(Exception ex){ex.printStackTrace();System.out.println(ex.getMessage());}return new ActionForward(mapping.findForward("success").getPath()+"?PARENT_ID=" + parent_id, true);}}
?以上注意最后的return。因为上传完要刷新页面,而上传我们是整个form提交,所以在前台不能像ajax请求一样在onComplete的时候去执行刷新,这时候就要通过这种方式来刷新。
ejb方面的代码:
public param_bean uploadFile(param_bean param)throws java.lang.Exception  {database_bean db = new database_bean();UserTransaction transaction = this.context.getUserTransaction();//获得JTA事务InputStream is = null;OutputStream out = null;try{transaction.begin();//开始JTA事务String id = (String)param.get_param("ID");String parent_id = (String)param.get_param("PARENT_ID");is = (InputStream)param.get_param("MULTIPART_OBJ");String multipart_type = (String)param.get_param("MULTIPART_TYPE");String multipart_name = (String)param.get_param("MULTIPART_NAME");String sql = "INSERT INTO EPUB.SYS_MULTIPART_FILE(ID,PARENT_ID,MULTIPART_OBJ,MULTIPART_TYPE,MULTIPART_NAME)" +" VALUES('"+id+"','"+parent_id+"',empty_blob(),'"+multipart_type+"','"+multipart_name+"')";db.execute_update(sql);String sqlblob = "SELECT MULTIPART_OBJ FROM EPUB.SYS_MULTIPART_FILE WHERE ID='"+id+"' FOR UPDATE";ResultSet rs = db.execute_query(sqlblob);if(rs.next()) {BLOB blob = (oracle.sql.BLOB)rs.getBlob(1);out = ((oracle.sql.BLOB)blob).getBinaryOutputStream();byte[] b = new byte[1024]; int len = 0; while( (len = is.read(b)) != -1){out.write(b,0,len); }out.flush();out.close();is.close();}transaction.commit();//提交JTA事务param.ret_code = 0;}catch(Exception ex){try{transaction.rollback();//回滚JTA事务}catch(SystemException se){se.printStackTrace();}param.ret_code = -1;ex.printStackTrace();}finally{db.close();}return param;    }
在以上代码中,注意的是?out.flush();out.close();is.close();一定要放在transaction.commit();//提交JTA事务之前,否则会报错,我一开始粗心就是放在finally里,其实应该是一out写入完后就close掉了。这点我一直没有搞清楚 。

下载写成一个servlet,这个servlet的代码如下:
public class ImgShowServlet extends HttpServlet{public static void writeImg(HttpServletResponse response,String img_id) {database_bean db = new database_bean();try {String sql = "SELECT MULTIPART_TYPE,MULTIPART_NAME,MULTIPART_OBJ FROM EPUB.SYS_MULTIPART_FILE WHERE ID='"+img_id+"'";ResultSet rs = db.execute_query(sql);if (rs.next()) {String multipart_type = rs.getString("MULTIPART_TYPE");String multipart_name = rs.getString("MULTIPART_NAME");Blob blob = rs.getBlob("MULTIPART_OBJ");String filename = new String(multipart_name.getBytes("GBK"), "iso-8859-1");String fileheader;if (multipart_type != null) {fileheader = "attachment; filename=" + filename + "."+ multipart_type;} else {fileheader = "attachment; filename=" + filename;}response.setHeader("Content-Disposition", fileheader);response.setContentType(UtilComAction.getContentType(multipart_type));byte[] b = new byte[1024];OutputStream os = response.getOutputStream();InputStream is = blob.getBinaryStream();int i = 0;while ((i = is.read(b)) != -1) {os.write(b, 0, i);}os.flush();os.close();is.close();}} catch (Exception e) {e.printStackTrace();}finally{db.close();}}public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{try{String img_id = request.getParameter("IMG_ID").toString();if((img_id==null||("".equals(img_id))||("null").equalsIgnoreCase(img_id))){throw new Exception("对不起!请确认传入参数img_id正确!");}ImgShowServlet.writeImg(response,img_id);}catch(Exception ex){ex.printStackTrace();}}public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{this.doGet(request, response);}}
?则下载的调用如下:/servletMgr/jsp/ImgShowServlet?IMG_ID='+id

热点排行