java.io.FileNotFoundException: http:\localhost:8080\ipms\upload\1332146087125.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()获取。