|
楼主 |
发表于 2018-4-13 14:26:25
|
显示全部楼层
我按照baas自定义action使用sql语句查询出二进制数据!然后再以文件流的形式写出!您看看我这段代码该如何修改才能成功获取数据库图片,自己写的代码也有点懵
// 获取图纸
public static JSONObject getImgInfo(JSONObject params, ActionContext context) throws JsonGenerationException, JsonMappingException, IOException {
String id = params.getString("Strid"); // 获取图纸ID
HttpServletRequest request = (HttpServletRequest) context.get(ActionContext.REQUEST);
HttpServletResponse response = (HttpServletResponse) context.get(ActionContext.RESPONSE);
PreparedStatement pstmt = null;
ResultSet rs = null;
Connection conn = null;
String sql = "";
JSONObject result = new JSONObject();
try {
conn = context.getConnection("txxt");
if (id != null) {
sql = "SELECT * FROM ENTITY_000 WHERE ID = '" + id + "' AND USED = 1";
System.out.println(sql);
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs != null) {
if (rs.next()) {
String ENTITY = rs.getString("ENTITY");
result.put("ENTITY", ENTITY);
System.out.println(ENTITY);
Blob blob = rs.getBlob("ENTITY_000");
InputStream ins = blob.getBinaryStream();
// 输出到文件
OutputStream fout = new FileOutputStream(ENTITY);
// 下面将BLOB数据写入文件
byte[] b = new byte[1024];
int len = 0;
while ((len = ins.read(b)) != -1) {
fout.write(b, 0, len);
}
response.setContentType("image/WMF");
response.setHeader("Cache-control", "no-cache");
// ServletOutputStream fout1 = response.getOutputStream();
int num;
byte buf[] = new byte[1024];
while ((num = ins.read(buf)) != -1) {
fout.write(buf, 0, num);
}
// 依次关闭
fout.close();
ins.close();
}
}
}
return null;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
} |
|