zoukankan      html  css  js  c++  java
  • FileNotFoundException: http:localhos46087125.jpg (文件名、目录名或卷标语法不正确

    java.io.FileNotFoundException: http:localhost:8080ipmsupload1332146087125.jpg (文件名、目录名或卷标语法不正确。)

    http://localhost:8080/ipms/upload/1332146087125.jpg这个是源路径,能打开;导出只能通过这个来获取图片;但是一导出,就报上面的那个错误,求解决方法;

    用户提供的答案1:

    ....io怎么通过http来访问...要本地物理路径 如  D:/tomcat/webapps/IPMS/UPLOAD/xxx.jpg

    用户提供的答案2:

    new ImageIcon(new URL("....")).getImage();

    用户提供的答案3:

    用request的contextPath拼上你后边的路径  /upload/1332146087125.jpg就可以拿到了

    用户提供的答案4:

    BufferedImage bufferImg = ImageIO.read(new URL(imageurl));

    我这样用的,可以用URL获取到图片

    可用方法:

     Java模块 -- 从网络中读取图片 转换成Base64字符串

     * 将图片转换为BASE64为字符串 
         * @param filename 
         * @return 
         * @throws IOException 
         */  
        public static String getImageString(String filename) throws IOException {    
            InputStream in = null;    
            byte[] data = null;    
            try {    
                //in = new FileInputStream(filename);   
                in = getInputStreamFromURL(filename);    
                data = new byte[in.available()];    
                in.read(data);    
                in.close();    
            } catch (IOException e) {    
                throw e;    
            } finally {    
                if(in != null) in.close();    
            }    
            BASE64Encoder encoder = new BASE64Encoder();    
            return data != null ? encoder.encode(data) : "";    
       }   
      
         /**  
         * 从URL中读取图片,转换成流形式.  
         * @param destUrl  
         * @return  
         */    
        public static InputStream getInputStreamFromURL(String destUrl){    
                
            HttpURLConnection httpUrl = null;    
            URL url = null;    
            InputStream in = null;     
            try{    
                url = new URL(destUrl);    
                httpUrl = (HttpURLConnection) url.openConnection();    
                httpUrl.connect();               
                in = httpUrl.getInputStream();              
                return in;    
            }catch (Exception e) {    
                e.printStackTrace();    
            }    
            return null;    
        }    
          
        public static void main(String[] args) {  
            String s = null;  
            try {  
                s = getImageString("http://localhost:8080/all/2017/02/09/14/0777a8af971040729998e6f3bd3b3dca.png");  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            System.out.println(s);  
        }

     本地图片获取InputStream用new FileInputStream(filename),

    网络图片需要用new URL(destUrl)处理,然后openConnection(),然后使用getInputStream()获取。

  • 相关阅读:
    17张程序员壁纸推荐,是否有一张你喜欢的?
    学会了这些英文单词,妈妈再也不用担心我学不会Python
    小白学习Python英语基础差怎么办,都帮你想好拉!看这里
    自动化测试学习防踩坑手册,测试人员人手一份
    Selenium自动化结合Mysql数据项目实战操作
    解除你学习Python自动化测试框架的所有疑惑,开启学习直通车
    数据库管理软件navicate12的激活和安装
    修改文件版本号方法
    Json的数据映射(基于LitJson)
    VMware 虚拟机安装黑屏问题
  • 原文地址:https://www.cnblogs.com/shamo89/p/9672662.html
Copyright © 2011-2022 走看看