spath + "" + specificate.getTitle(); 路径加 文件名取到文件。
public void preview() {
String spath = ServletActionContext.getServletContext().getRealPath("")
+ "contentfile";
specificate = (Specificate) baseService.getById(Specificate.class, id);
String fileName = specificate.getTitle();
String fileNameWithPath = spath + "" + specificate.getTitle();
log.info("文件名=" + fileName);
// File file = new File(fileName);
try {
// 转码(UTF-8-->GB2312),现在环境下的编码是UTF-8,但
服务器操作系统的编码是GB2312
if (fileName != null && fileName.trim().length() > 0) {
fileName = URLEncoder.encode(fileName, "GB2312");
fileName = URLDecoder.decode(fileName, "ISO8859-1");
} else {
fileName = "moren.pdf";
fileNameWithPath = spath + "" + fileName;
}
File file = new File(fileNameWithPath);
FileInputStream fileinputstream = new FileInputStream(file);
long l = file.length();
int k = 0;
byte abyte0[] = new byte[65000];
getResponse().setContentType("application/pdf");
getResponse().setContentLength((int) l);
getResponse().setHeader("Content-Disposition",
"inline; filename=" + fileName);
while ((long) k < l) {
int j;
j = fileinputstream.read(abyte0, 0, 65000);
k += j;
getResponse().getOutputStream().write(abyte0, 0, j);
}
fileinputstream.close();
} catch (IOException e) {
log.error("打开失败,
文件路径为:" + fileNameWithPath, e);
e.printStackTrace();
// return this.ajaxText("打开失败");
}
// return "";
}
<input type="button" id="<s:property value='spId'/>" class="btn btn-success" value="预览" onclick="viewPDF(this)" />
<script type="text/
javascript">
function viewPDF(cur){
// alert($(cur).attr('id'));
var fileid = $(cur).attr('id');
var strURL = "preview.action?id="+fileid;
var sheight = screen.height-70;
var swidth = screen.width-10;
var winoption="left=0,top=0,height="+sheight+",width="+swidth+",toolbar=yes,menubar=yes, location=yes,status=yes,scrollbars=yes,resizable=yes";
var tmp=window.open(strURL,'',winoption);
}
</script>