qq_35232040
qq_35232040
采纳率100%
2017-02-28 05:50

请教一个struts2下载oracle数据库blob字段的问题

200
已采纳

action:

public String download()
    {
        Long docId = Long.valueOf(RequestUtil.getParam(request, "docId", new Integer(0))).longValue();
        try 
        {  
            inputStream = this.taDocumentService.downLoad(docId);  
            return SUCCESS;
        } 
        catch (Exception e) 
        {  
            e.printStackTrace();
            return ERROR;
        }
    }

service:

public InputStream downLoad(Long docId)
{
    TaDocument taDocument = this.read(docId);
    String filepath = "E:/" + taDocument.getDocName() +"."+ taDocument.getDocType();
    InputStream inputStream = null;
    try
    {
        Blob blob = taDocument.getDocContent();
        if(blob != null)
        {
            inputStream = blob.getBinaryStream();
            FileOutputStream file = new FileOutputStream(filepath);
            int len = (int) blob.length();
            byte[] buffer = new byte[len];
            while ( (len = inputStream.read(buffer)) != -1) 
            {
                file.write(buffer, 0, len);
            }
            file.close();
            inputStream.close();
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return inputStream;
}

配置文件:

<package name="taDocument" namespace="/taDocument" extends="power-default">
    <action name="*" class="com.power.web.action.manager.TaDocumentAction" method="{1}">
        <result name="success" type="stream"> 
            <param name="contentType">application/octet-stream;charset=ISO8859-1</param> 
            <param name="inputName">inputStream</param> 
            <param name="contentDisposition">attachment;filename="${attachmentFileName}"</param>     
            <param name="bufferSize">4096</param>    
        </result>
    </action>
</package>

请问为什么不能正常弹出文件下载框并下载呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • HackAge4619 写给初学者的几封信 4年前
    点赞 评论 复制链接分享
  • qq_35232040 qq_35232040 4年前

    public String download()throws Exception{
    Long docId = Long.valueOf(RequestUtil.getParam(request, "docId", new Integer(0))).longValue();
    try
    {

    inputStream = this.taDocumentService.downLoad(docId);

    return SUCCESS;
    }
    catch (Exception e)
    {

    e.printStackTrace();
    return ERROR;
    }
    }

    
    
    点赞 评论 复制链接分享
  • qq_35232040 qq_35232040 4年前

    需求是弹出一个文件框可以自由选择路径并且保存。求大神指点

    点赞 评论 复制链接分享

相关推荐