在 jsp 中,我从 BD 获取文件并希望在客户端下载它:
<%
String num = request.getParameter("param");
Statement sta = null;
sta = conn.createStatement();
String fileName="";
String sql=("SELECT files,filename FROM filestock WHERE num =(SELECT filestock_id FROM parcels_temp WHERE num="+num+")");
ResultSet rs=sta.executeQuery(sql);
while(rs.next()){
byte[] file = rs.getBytes("files");
fileName=rs.getString("filename");
}
FileOutputStream fs = new FileOutputStream(new File(fileName));
BufferedOutputStream bs = new BufferedOutputStream(fs);
bs.write(file);
bs.close();
fs.close();
rs.close();
ps.close();
%>
我将PDF文件。所以我有一些问题:
1. 我将如何处理文件以将其发送到 JavaScript。
2. 我可以使用 ExtJs 3.4 或 JavaScript 保存这个文件吗?
更新
现在我尝试将文件从服务器发送到客户端:
<%
String num = request.getParameter("param");
Statement sta = null;
sta = conn.createStatement();
String fileName="";
byte[] file=null;
int bufferSize = 8192;
String sql=("SELECT files,filename FROM filestock WHERE num =(SELECT filestock_id FROM parcels_temp WHERE num="+num+")");
ResultSet rs=sta.executeQuery(sql);
while(rs.next()){
file = rs.getBytes("files");
fileName=rs.getString("filename");
}
File dFile=new File(fileName);
InputStream in1 = request.getInputStream();
int read;
while ((read = in1.read(file, 0, bufferSize)) != -1) {
out.write(file, 0, read);
}
sta.close();
rs.close();
conn.close();
%>
但得到错误:
The method write(char[], int, int) in the type Writer is not applicable for the arguments (byte[], int, int)
那么该怎么做呢?
更新2
使用此代码我没有收到任何错误,但在萤火虫中我看到服务器没有发送给客户端:
<%
String num = request.getParameter("param");
Statement sta = null;
sta = conn.createStatement();
String fileName="";
byte[] file=null;
int bufferSize = 8192;
String sql=("SELECT files,filename FROM filestock WHERE num =(SELECT filestock_id FROM parcels_temp WHERE num="+num+")");
ResultSet rs=sta.executeQuery(sql);
while(rs.next()){
file = rs.getBytes("files");
fileName=rs.getString("filename");
}
//File dFile=new File(fileName);
FileOutputStream fout = fout = new FileOutputStream(fileName);
//BufferedInputStream in1 = new BufferedInputStream(fout);
InputStream in1 = request.getInputStream();
int read;
while ((read = in1.read(file, 0, bufferSize)) != -1) {
fout.write(file, 0, read);
}
sta.close();
rs.close();
conn.close();
%>