Java 从BLOB字段下载文件

Java 从BLOB字段下载文件,java,jsp,encoding,base64,blob,Java,Jsp,Encoding,Base64,Blob,我有一个应用程序,在其中我将文件(doc、pdf、xls、txt)存储在DB2数据库(用base64编码)中的BLOB字段上。现在我必须下载这个文件,只有当我下载一个文本文件时,我才能成功。当我下载其他文件时,我无法正确解码它们 我尝试使用两种方法在BLOB文件中插入内容: data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,RW5yaWNvIEJlcmdhbW8= 及 在这两种情况下,结果

我有一个应用程序,在其中我将文件(doc、pdf、xls、txt)存储在DB2数据库(用base64编码)中的BLOB字段上。现在我必须下载这个文件,只有当我下载一个文本文件时,我才能成功。当我下载其他文件时,我无法正确解码它们

我尝试使用两种方法在BLOB文件中插入内容:

data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,RW5yaWNvIEJlcmdhbW8=

在这两种情况下,结果对我来说都是一样的

我用来下载和解码这些文件的代码是:

fileDownload.jsp

<%@ page import="java.io.*"%>
<%@ page import="com.ibm.misc.*"%>

<%
    String fileName = request.getParameter("fileName");
    String fileType = request.getParameter("fileType");
    String fileContent = b64Decode(request.getParameter("fileContent"));

    response.setContentType(fileType);
    //response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=\""
            + fileName + "\"");
    response.setContentLength((int) fileContent.length());

    try {

        StringBuffer sb = new StringBuffer(fileContent);
        InputStream in = new ByteArrayInputStream(sb.toString().getBytes());
        //InputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
        ServletOutputStream sos = response.getOutputStream();

        byte[] outputByte = new byte[4096];
        //copy binary contect to output stream
        while (in.read(outputByte, 0, 4096) != -1) {
            sos.write(outputByte, 0, 4096);
        }
        in.close();
        sos.flush();
        sos.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
%>

<%!public String b64Decode(String msg) {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] decodedBytes = null;
        try {
            decodedBytes = decoder.decodeBuffer(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(decodedBytes);
    }%>


我相信问题在于编码,但我不知道如何解决这个问题。

你真的不应该使用JSP来解决这个问题。JSP旨在呈现HTML之类的内容,而不是二进制数据。JSP容器将在幕后进行自己的字符编码和内容类型的诡计,这可能与您的意图相冲突


您应该将其重写为一个servlet,而不是JSP,并且它应该更可预测。

首先,感谢您的快速回答。我以前从未使用过JSP/Servlets,因此在这项技术中,我不知道执行这些任务的最佳方法,您是否知道一段代码供我使用?不幸的是,问题仍然存在。细节是,我的blob是一个DataURL,我花了一整天的时间寻找一种方法,使用JSP、Java或Javascript将DataURL转换回文件。但仍然没有找到答案。你有没有遇到过这个问题?
<%@ page import="java.io.*"%>
<%@ page import="com.ibm.misc.*"%>

<%
    String fileName = request.getParameter("fileName");
    String fileType = request.getParameter("fileType");
    String fileContent = b64Decode(request.getParameter("fileContent"));

    response.setContentType(fileType);
    //response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=\""
            + fileName + "\"");
    response.setContentLength((int) fileContent.length());

    try {

        StringBuffer sb = new StringBuffer(fileContent);
        InputStream in = new ByteArrayInputStream(sb.toString().getBytes());
        //InputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
        ServletOutputStream sos = response.getOutputStream();

        byte[] outputByte = new byte[4096];
        //copy binary contect to output stream
        while (in.read(outputByte, 0, 4096) != -1) {
            sos.write(outputByte, 0, 4096);
        }
        in.close();
        sos.flush();
        sos.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
%>

<%!public String b64Decode(String msg) {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] decodedBytes = null;
        try {
            decodedBytes = decoder.decodeBuffer(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(decodedBytes);
    }%>