此问题可以在tomcat服务上修改server.xml文件,
1.在host标签下添加如下内容
<Context path="/" docBase="F:\testTIFF" debug="0" reloadable="true"></Context>
2.第二种方法
jsp页面:
img2 += '<img src="exhequipmentController.do?viewFileNew&filePath='
+ logo //图片地址
+ '" width="200px;" height="200px;"border="1" alt="">';
$("#fileZmName2").html(img2);
java后台代码:
@RequestMapping(params = "viewFileNew")
public void viewFileNew(HttpServletRequest request, HttpServletResponse response) throws Exception {
String url=org.jeecgframework.core.util.StringUtil.encodeToUTF8(request.getParameter("path")) ;
String url1 = new String(request.getParameter("filePath").getBytes("iso-8859-1"), "utf-8");
url=StringUtil.getEncodePra(url1);
String rootPath = ToolUtil.getString("fileLocal");// 获取服务器下gh_bfm项目的照片路径
UploadFile uploadFile = new UploadFile(request, response);
String path = rootPath+"\\"+url;
String extend = path.substring(path.lastIndexOf(".")+1, path.length());
String attachmenttitle="";
File cat=new File(path);
if(cat.exists()){
BufferedInputStream in = new BufferedInputStream(new FileInputStream(path));
ByteArrayOutputStream out = new ByteArrayOutputStream();
int size = 0;
byte[] temp = new byte[1024];
while((size = in.read(temp))!=-1) {
out.write(temp, 0, size);
}
in.close();
uploadFile.setContent(out.toByteArray());
uploadFile.setExtend(extend);
uploadFile.setTitleField(attachmenttitle);
uploadFile.setRealPath(null);
int index = path.lastIndexOf("\\") == -1 ? path.lastIndexOf("/") : path.lastIndexOf("\\");
attachmenttitle = path.substring(index + 1, path.lastIndexOf("."));
}else {
uploadFile.setContent(null);
uploadFile.setExtend(null);
uploadFile.setTitleField(null);
uploadFile.setRealPath(null);
}
systemService.viewOrDownloadFile(uploadFile);
}
/**
* 文件下载或预览
*
* @param request
* @throws Exception
* @throws Exception
*/
@SuppressWarnings("unchecked")
public HttpServletResponse viewOrDownloadFile(UploadFile uploadFile) throws Exception {
uploadFile.getResponse().setContentType("UTF-8");
uploadFile.getResponse().setCharacterEncoding("UTF-8");
InputStream bis = null;
BufferedOutputStream bos = null;
HttpServletResponse response = uploadFile.getResponse();
HttpServletRequest request = uploadFile.getRequest();
String userAgent = request.getHeader("User-Agent");
String ctxPath =uploadFile.getRequest().getSession().getServletContext().getRealPath("/") + (StringUtil.isNotEmpty(uploadFile.getBasePath()) ? uploadFile.getBasePath():ResourceUtil.getConfigByName("uploadpath"));
String downLoadPath = "";
long fileLength = 0;
if (uploadFile.getRealPath() != null&&uploadFile.getContent() == null) {
//禁止全路径下载 L.T 15. 07.01
if (uploadFile.getRealPath().toLowerCase().indexOf("d:")==0||
uploadFile.getRealPath().toLowerCase().indexOf("c:")==0||
uploadFile.getRealPath().toLowerCase().indexOf("e:")==0||
uploadFile.getRealPath().toLowerCase().indexOf("f:")==0){
throw new Exception("非法路径!");
}
//禁止全路径下载 L.T 15. 07.01 end
downLoadPath = ctxPath + uploadFile.getRealPath();
fileLength = new File(downLoadPath).length();
try {
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
if (uploadFile.getContent() != null)
bis = new ByteArrayInputStream(uploadFile.getContent());
fileLength = uploadFile.getContent().length;
}
// String filename="";
try {
if (!uploadFile.isView() && uploadFile.getExtend() != null) {
if (uploadFile.getExtend().equals("text")) {
response.setContentType("text/plain;");
} else if (uploadFile.getExtend().equals("doc")) {
response.setContentType("application/msword;");
} else if (uploadFile.getExtend().equals("xls")) {
response.setContentType("application/ms-excel;");
} else if (uploadFile.getExtend().equals("pdf")) {
response.setContentType("application/pdf;");
} else if (uploadFile.getExtend().equals("jpg") || uploadFile.getExtend().equals("jpeg")) {
response.setContentType("image/jpeg;");
} else {
response.setContentType("application/x-msdownload;");
}
if (userAgent.contains("Safari")) {
response.setHeader("Content-disposition", "attachment; filename=\"" + new String((uploadFile.getTitleField() + "." + uploadFile.getExtend()).getBytes("UTF-8"), "ISO8859-1") + "\"");
} else {
response.setHeader("Content-disposition", "attachment; filename=\"" + new String((uploadFile.getTitleField() + "." + uploadFile.getExtend()).getBytes("GBK"), "ISO8859-1") + "\"");
}
response.setHeader("Content-Length", String.valueOf(fileLength));
}
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}