我需要从 MySQL DB 检索 .doc 文件,该文件以 BLOB 数据类型存储。这是我的操作文件代码
List<CandyDetails> det = new ArrayList<CandyDetails>();
CandyDetails details = new CandyDetails();
ResultSet rs3 = st.executeQuery("SELECT * FROM seek_resume WHERE seek_code = '"+seekCode+"'");
while(rs3.next()){
details.setResume(rs3.getBytes(2));
}
request.setAttribute("candyDet", det);
return mapping.findForward("success");
这是 CandyDetails.java 文件
private byte[] resume;
//getter/setter methods
这是我的JSP页面代码
<logic:iterate name="candyDet" id="in">
${in.resume}
</logic:iterate>
我在 jsp 页面上得到的输出是一些乱码
[B@3efc0688
每次刷新时,B@后面的数字似乎都在变化。
我使用的是 Struts 1.3。
最佳答案
将其放入 Action 类中
String qry="SELECT * FROM seek_resume WHERE seek_code = '"+seekCode+"'"";
Statement st = con.createStatement(qry);
ResultSet rs = st.executeQuery();
if(rs.next()) //show one document
{
byte[] bytearray = new byte[1048576];
int size=0;
InputStream is = rs.getBinaryStream(2);
// String contentType=rs.getString("content_type");
String contentType = "application/msword"; //doc
String contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; //docx
response.reset();
response.setContentType(contentType);
while((size=is.read(bytearray))!= -1 )
{
response.getOutputStream().write(bytearray,0,size);
}
}
//return null;
----------jsp--------
<a href="showDoc.do?seekCode=${seekCode}" target="_blank"> View</a>
//根据需要放置您的链接。
通过将代码传递给操作类来调用操作类,然后会给您文档作为响应。
或者
您可以使用 Apache POI 将文档显示为内联内容。
关于java - 使用 Struts 在 JSP 中显示来自 BLOB MySQL 的文档文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23457174/