java关于img标签图片访问不到的问题

本文介绍在Tomcat服务中实现图片预览与下载的方法,包括修改server.xml配置及使用Java后台代码处理图片请求的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此问题可以在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;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值