FormFile image = infoForm.getImage();//取得上传的文件 
String size = (image.getFileSize() + " bytes");//文件大小 
String fileName= image.getFileName();//文件名 
String contentType = image.getContentType(); 
String aa="";
try { 
  InputStream stream = image.getInputStream();//把文件读入 
String filePath = "D:/platenews/platenews/upload";//
request.getRealPath("/upload");//取当前系统路径 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
OutputStream bos = new FileOutputStream(filePath + "/" +  image.getFileName()); 
              //建立一个上传文件的输出流,将上传文件存入web应用的根目录。 
aa = filePath+"/"+image.getFileName(); 
int bytesRead = 0; 
 byte[] buffer = new byte[8192]; 
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) { 
 bos.write(buffer, 0, bytesRead);//将文件写入服务器 
 } 
bos.close(); 
stream.close();
 }catch(Exception e){ 
 System.err.print(e); 
} 我用这个方法上传照片,照片能传到指定文件夹里,但是我不知道应该怎么样把路径存到数据库里,也就是里面的String aa,存数据库不允许是“\”而我用aa.replaceAll("/","")转换后还是不行。
这个程序主要实现的是上传图片到文件,然后在web页面显示。
我的思路是存到文件,然后得到文件保存路径然后把路径存到数据表里,再在web页面显示时候读出路径而显示出照片。
朋友路过的话帮忙看看:1、我的思路行得通不,是不是有更好点的方法。2、我取的aa的值怎么样保存到数据表里(varchar)。3、我用的是eclipse开发,在取当前路径(String filePath)时,我用String filePath = request.getRealPath("/"); 来取eclipse提示说不赞成用这个方法,我找不到好的其他方法,麻烦指导。

解决方案 »

  1.   

    我得到的String filePath="D:/platenews/platenews/upload" 我把他付值给一个form.setImageFile(filePath)不行。form里定义的imageFile是string的。所以有点纳闷的了
      

  2.   

    aa = filePath+"/"+image.getFileName(); 
    改为:
    aa = filePath+"\\\\"+image.getFileName(); 楼主拿分来吧
      

  3.   

    不用\\,
    用文件分隔符File.pathSeparatorChar或File.pathSeparator试试!
      

  4.   

    数据库存储没有 “\” 的限制。是不是自己创建数据库的时候添加了限制条件。或是程序中有check?如果就几个路径的话,把路径放到配置文件中,数据库里放文件名。