在ofbiz体系中如何实现文件下载?

03-09-26 dalianzj
我用ofbiz做一个oa,需要用到文件的上传下载,上传我已经做完了,但下载始终没有实现!我用的javaBean是jspsmartupload,下载时如果是word文档或.rar文件,浏览器总会自动打开,显示乱码!而且出现getOutputStream called错误!希望高手能帮我看一下如何实现文件下载!不胜感谢!

download.jsp如下:

<%@ page language="java" import="com.jspsmart.upload.*"%>

<%@ page import="java.util.*, org.ofbiz.core.service.ModelService,java.sql.* " %>

<%@ page import="org.ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>

<%@ page import="org.ofbiz.core.entity.*" %>

<meta http-equiv="Content-Type" content="application/msword; charset=gb2312">

<jsp:useBean id="delegator" type="org.ofbiz.core.entity.GenericDelegator" scope="request" />

<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />

<%!

String docName = ""; 需要下载的文件名

int docSize = 0;

Map document = new HashMap();

LinkedList docList = null;

%>

<%

try

{

List b = delegator.findAll("ProcessDoc",UtilMisc.toList("docName"));

docList = new LinkedList(b);

}catch(Exception ex){System.out.println("查找数据库是出错!");}

try

{

if(docList!=null) docSize = docList.size();

if(docSize!=0)

{

document = (Map)docList.get(docSize-1);

docName = (String)document.get("docName");

}

}catch(Exception e){

System.out.println("取值时候出错!");

System.out.println(e.getMessage());

e.printStackTrace();

}

// Initialization

mySmartUpload.initialize(pageContext);

// Download file

mySmartUpload.downloadFile("/upload/"+docName);

%>

dalianzj
2003-09-26 10:58
<%@ page language="java" import="com.jspsmart.upload.*"%>

<%@ page import="java.util.*, org.ofbiz.core.service.ModelService,java.sql.* " %>

<%@ page import="org.ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>

<%@ page import="org.ofbiz.core.entity.*" %>

<meta http-equiv="Content-Type" content="application/msword; charset=gb2312">

<jsp:useBean id="delegator" type="org.ofbiz.core.entity.GenericDelegator" scope="request" />

<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />

<%!

String docName = ""; 需要下载的文件名

int docSize = 0;

Map document = new HashMap();

LinkedList docList = null;

%>

<%

try

{

List b = delegator.findAll("ProcessDoc",UtilMisc.toList("docName"));

docList = new LinkedList(b);

}catch(Exception ex){System.out.println("查找数据库是出错!");}

try

{

if(docList!=null) docSize = docList.size();

if(docSize!=0)

{

document = (Map)docList.get(docSize-1);

docName = (String)document.get("docName");

}

}catch(Exception e){

System.out.println("取值时候出错!");

System.out.println(e.getMessage());

e.printStackTrace();

}

// Initialization

mySmartUpload.initialize(pageContext);

// Download file

mySmartUpload.downloadFile("/upload/"+docName);

%>

dalianzj
2003-09-26 11:01
代码如何显示?

// 在此输入java代码
<%@ page language="java" import="com.jspsmart.upload.*"%>
<%@ page import="java.util.*, org.ofbiz.core.service.ModelService,java.sql.* " %>
<%@ page import="org.ofbiz.core.util.*, org.ofbiz.core.pseudotag.*" %>
<%@ page import="org.ofbiz.core.entity.*" %>
<meta http-equiv="Content-Type" content="application/msword; charset=gb2312"> 
<jsp:useBean id="delegator" type="org.ofbiz.core.entity.GenericDelegator" scope="request" />
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%!
  String docName = "";                 需要下载的文件名
  int docSize = 0;
  Map document = new HashMap();
  LinkedList docList = null;
%>
<%
  try
  {
    List b = delegator.findAll("ProcessDoc",UtilMisc.toList("docName"));
    docList = new LinkedList(b);
  }catch(Exception ex){System.out.println("查找数据库是出错!");}
  try
  {
    if(docList!=null) docSize = docList.size();
    if(docSize!=0)
    {
      document = (Map)docList.get(docSize-1);
      docName = (String)document.get("docName");                    
    }
  }catch(Exception e){
   System.out.println("取值时候出错!");
   System.out.println(e.getMessage());
   e.printStackTrace();
  }
	
	// Initialization
	mySmartUpload.initialize(pageContext);
	
	// Download file
	mySmartUpload.downloadFile("/upload/"+docName);
	
	

%>
<p>

wys1978
2003-09-26 18:45
由于不同的servlet container在jsp的contentType和setHeader上处理不一样,所以我不推荐在jsp页面上使用download的代码。

我在ofbiz的架构上是这样处理的,upload file以后,将文件的content Type ,file path 等一些基本信息save起来,以下为Entity定义:

  <entity entity-name="Attachment"
            package-name="com.smics.pub"
            title="Attachment Entity">
        <field name="attachmentId" type="numeric"/>
        <field name="attachmentType" type="id-ne"/>
        <field name="path" type="long-varchar"/>
        <field name="contentType" type="mid-varchar"/>
        <field name="fileName" type="mid-varchar"/>
        <field name="fileSize" type="numeric"/>
        <field name="version" type="numeric"/>
        <prim-key field="attachmentId"/>
  </entity>
<p>

在request的时候,是在java里面处理,不返回jsp,一下是controller的定义:

    <request-map uri="viewAttachment">
    	<security https="true" auth="true"/>
    	<event type="java" path="com.smics.util.attachment.DownloadEvents" invoke="download"/>
    	<response name="success" type="none"/>
    </request-map>
<p>

在java代码里面利用entity的contentType和path来处理,以下是java code:

/*
 * Created on 2003-8-18
 */
package com.smics.util.attachment;

/**
 * @author Quake Wang
 */

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.ofbiz.core.util.*;
import org.ofbiz.core.entity.*;

public class DownloadEvents {
	public static final String module = DownloadEvents.class.getName();
	
	public static String download(HttpServletRequest request, HttpServletResponse response) throws Exception{
		//get attachment info from database
		GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
		String attachmentId = request.getParameter("attachmentId");
		//TODO: NPE check
		
		GenericValue attachment = null;
                  try {
                      attachment = delegator.findByPrimaryKeyCache("Attachment", UtilMisc.toMap("attachmentId", attachmentId));
                  } catch (GenericEntityException gee) {
			Debug.logError(gee, module);
			return "error";
                  }	
		File f = new File(attachment.getString("path"));
		FileInputStream in = new FileInputStream(f);		
		
		response.setContentType(attachment.getString("contentType"));
		response.setHeader("Content-Disposition", "filename="+attachment.getString("fileName"));
		response.setContentLength((int)f.length());
				
		//fetch the file
	 	int length = (int)f.length();
	 	if(length != 0)  {
	   		byte[] buf = new byte[4096]; 
	   		ServletOutputStream op = response.getOutputStream();
			while ((in != null) && ((length = in.read(buf)) != -1))  {
				op.write(buf,0, length);
			}
		   	in.close();
	   		op.flush();
	   		op.close();
	 	}
	 		
		//TODO: log hit
		return "success";
	}
}
<p>

redlly
2004-11-24 05:04
在OA系统中一个最终常见的重要的功能就是公文流转,在这种应用中如果我需要传递的一个公文(例如是word文档),而这个文档要流转到各个审批者手中进行审阅并进行修改(同时要支持文档的电子签名,既保证每一个文档的修改者所作的操作都被记录,如果他修改了其他操作者的纪录也应该被记录到),这里不但涉及到文件的下载上传还要涉及到文件的修改和签名,这种功能是否能够实现?

猜你喜欢