DB2データベース(base64でエンコード)のBLOBフィールドにファイル(doc、pdf、xls、txt)を格納するアプリケーションがあります。今、私はファイルをダウンロードしなければなりません、そして、私はテキストファイルをダウンロードするときだけ成功しています。私が他のファイルをダウンロードすると、私はそれらを正しくデコードできません。BLOBフィールドからファイルをダウンロード
私は、次の2つの方法を使用してBLOBファイルの内容を挿入しようとした:
data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,RW5yaWNvIEJlcmdhbW8=
と
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 /サーブレットで作業していませんでした。この技術では、これらのタスクを実行するための最善の方法はわかりません。作業するコードを知っていますか? – eBergamo
残念ながら、問題はまだここにあります。詳細は、私のBLOBがDataURLなので、私はJSPやJava、Javascriptを使ってdataURLをFileに変換する方法を探していました。しかし答えは見つかりませんでした。あなたはこの問題を経験しましたか? – eBergamo