获取图片路径问题

musecangying007 2009-11-18 08:20:51
前台页面
图片<input type="file" name="path" size="29">

action类
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
FileInputStream in = null;
try {
in = new FileInputStream(this.getPath());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Blob photo = null;
try {
photo = Hibernate.createBlob(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



我在自己的电脑上可以获得图片路径并将图片保存到数据库
但在局域网其它机器上登陆页面并向数据库插入图片时报下面的错误
java.io.FileNotFoundException: D:\My Documents\My Pictures\1024x768.jpg (系统找不到指定的文件。)
...全文
137 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
musecangying007 2009-11-19
  • 打赏
  • 举报
回复
1楼说的对,我应该把path定义为File类,问题以解决,谢谢各位
zl3450341 2009-11-18
  • 打赏
  • 举报
回复
String path=ServletActionContext.getServletContext().getRealPath("/fileDir");
path=path+fileName;
System.out.println(myFile);
try {
BufferedInputStream input=new BufferedInputStream(new FileInputStream(myFile),2048);
BufferedOutputStream output=new BufferedOutputStream(new FileOutputStream(new File(path)),2048);

byte [] b=new byte[2048];
while(input.read(b)>1){
output.write(b);
b=new byte[2048];
}

input.close();
output.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

把图片存到文件夹里面 数据库里面存路径
上面给你个例子 存到文件夹里面
javaors 2009-11-18
  • 打赏
  • 举报
回复
要写相对路径,不要写死路径,不然这个程序的移植性也会很差的
andesen 2009-11-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sangshusen_1988 的回复:]
建个ActionForm,使用formfile去接数据,顺便设置form的enctype=multipart/form-data
[/Quote]
用这个方法可行
sangshusen_1988 2009-11-18
  • 打赏
  • 举报
回复
建个ActionForm,使用formfile去接数据,顺便设置form的enctype=multipart/form-data
sangshusen_1988 2009-11-18
  • 打赏
  • 举报
回复
啥意思?你传过字符串过来干嘛??

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧