10,606
社区成员
发帖
与我相关
我的任务
分享
@RequestMapping("/read.bo")
public void read(HttpServletRequest request, HttpServletResponse response,
BaseRequest brqs) {
OutputStream os = null;
try {
// 验证参数
String id = request.getParameter("id");
if (id == null || id.length() == 0) {
throw new WAFException("参数缺失!");
}
AttachmentVO attachmentVO = getService().getVOById(id);
if (attachmentVO == null) {
throw new WAFException("参数缺失!");
}
String path = attachmentVO.getUrl() + File.separator
+ attachmentVO.getPhysicalname();
String fileRealPath = request.getServletContext().getRealPath(path);
File file = new File(fileRealPath);
String filename = attachmentVO.getSourcename();
if (!file.exists()) {
throw new WAFException("阅读的附件不存在!");
}
// 读取文件
InputStream fis = new BufferedInputStream(new FileInputStream(
fileRealPath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
// 设置下载文件名等
String showFileName = new String(filename.replaceAll(" ", "")
.getBytes("utf-8"), "iso8859-1");
String extName = showFileName.substring(
showFileName.lastIndexOf(".")).toLowerCase();
response.addHeader("Content-Disposition", "attachment;filename="
+ showFileName);
response.addHeader("Content-Length", "" + file.length());
// 设置内容编码
response.setCharacterEncoding(WAFConstants.CHARSET_DEFAULT);
// 设置响应MIME
if (".doc".equals(extName)) {
response.setContentType("application/msword");
} else if (".docx".equals(extName)) {
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
} else if (".pdf".equals(extName)) {
response.setContentType("application/pdf");
} else if (".xls".equals(extName)) {
response.setContentType("application/vnd.ms-excel");
} else if (".xlsx".equals(extName)) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
} else if (".ppt".equals(extName)) {
response.setContentType("application/vnd.ms-powerpoint");
} else if (".pptx".equals(extName)) {
response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation");
} else if (".bmp".equals(extName)) {
response.setContentType("image/bmp");
} else if (".gif".equals(extName)) {
response.setContentType("image/gif");
} else if (".ief".equals(extName)) {
response.setContentType("image/ief");
} else if (".jpeg".equals(extName)) {
response.setContentType("image/jpeg");
} else if (".jpg".equals(extName)) {
response.setContentType("image/jpeg");
} else if (".png".equals(extName)) {
response.setContentType("image/png");
} else if (".tiff".equals(extName)) {
response.setContentType("image/tiff");
} else if (".tif".equals(extName)) {
response.setContentType("image/tif");
}
os = new BufferedOutputStream(response.getOutputStream());
os.write(buffer);// 输出文件
os.flush();
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
if (os != null) {
try {
os.write(e.getLocalizedMessage().getBytes(
WAFConstants.CHARSET_DEFAULT));
os.flush();
} catch (Exception le) {
// NOOP
}
}
} finally {
if (os != null) {
try {
os.close();
} catch (Exception e) {
// NOOP
}
}
}
}
$(function(){
var id = "${id}";
var url = waf.contextPath + "/bpms/peform/core/attachment/read.bo?id=" + id;
$("#v_waf_bpms_attach_read").html("<iframe width='100%' height='100%' src='" + url + "' />");
})