|
ActionServlet中重写process,加了一行
request.setCharacterEncoding("GB2312");
action类中下载方法如下:
private boolean download(String id,HttpServletRequest request,HttpServletResponse response){
// 设置响应头和下载保存的文件名
response.reset();
response.setContentType("APPLICATION/OCTET-STREAM;charset=GB2312");
// try{
response.setHeader("Content-Disposition",
"attachment; filename=\"" +id + ".jar\"");
// }catch(UnsupportedEncodingException e){
// System.out.println(e);
// }
if(debug){
System.out.println("download at ");
}
boolean b = false;
DbConnection db = null;
Connection conn = null;
Statement st = null;
String sql = "select CONTENT from DIC_PLUGINS where id = '"+id+"'";
if(debug){
System.out.println(sql);
}
ResultSet rs = null;
Blob blob = null;
try{
db = new DbConnection();
db.openConnection();
conn = db.getConnection();
st = conn.createStatement();
rs = st.executeQuery(sql);
if(rs.next()){
blob = rs.getBlob("CONTENT");
}
}catch(Exception e){
System.out.println(e);
}finally{
try{
if(rs != null)
rs.close();
if(st!=null)
st.close();
if(conn != null)
conn.close();
if(db != null)
db.close();
}catch(Exception e){
System.out.println(e);
}
}
if(debug){
System.out.println("write");
}
// 写出流信息
if(blob!=null){
InputStream fileInputStream=null;
int size=0;
try{
fileInputStream = blob.getBinaryStream();
size = (int)blob.length();
}catch(SQLException e){
System.out.println(e);
}
byte[] bytes = new byte[size];
// fileInputStream.read(bytes);
int i=0;
if(debug){
System.out.println("last");
}
try{
while ((i=fileInputStream.read(bytes)) != -1) {
response.getOutputStream().write(bytes,0,i);
}
fileInputStream.close();
response.getOutputStream().flush();
b = true;
}catch(Exception e){
System.out.println(e);
}
}
return b;
} |
|