你好,我遇到一个图片无法上传到服务器的问题,想请教你。情况是这样的,我用jspSmartUpload做的上传文件到服务器的mySQL数据库里,程序原来在我自己的机器上调试都很好,当我把程序发到服务器上后就出现了问题:我向服务器MySQL数据库添加新记录时候,我输入的一些文本信息,例如,名称,图片描述等等,都能正常的添加并保存在数据库中,只有当我从本地上载一个图片文件时候出现错误,提示说org.apache.jasper.JasperException: An exception occurred processing JSP page /upload.jsp at line 59 56: //store the upload file in the databank 
57: 
58: file = new java.io.File(fullName); 
59: fis = new java.io.FileInputStream(file); 
60: // out.println(file.length()); 
61: } 
62: //open the databank java.io.FileNotFoundException: F:\private\bank\bank\images\bg.jpg (No such file or directory) 
java.io.FileInputStream.open(Native Method) 
java.io.FileInputStream. <init>(FileInputStream.java:106) 
org.apache.jsp.upload_jsp._jspService(upload_jsp.java:115) 
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) 
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) 
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)图片文件添加的jsp是这样的:
<form name="form1" method="post" action="upload.jsp" enctype="multipart/form-data">
......<tr>
      <td height="87"><h3>Picture:</h3></td>
      <td width="506">
        <input  name="image1" type="file" id="image1" onchange="javascript:PreviewPhotoatwidth(this,picpreview,260)" /> </td>        <td width="140" align="center"><p><img id="picpreview" alt='Preview' /></p>        </td>      
      <td width="12"></td>
    </tr>
我的upload.jsp中代码是这样的:
<%@ page contentType="text/html;charset=gb2312"%><%@ page import="com.jspsmart.upload.*" %>
<%@ page import="java.sql.*"%>
<%@ page import="com.hubin.bean.DataBean"%>
<%--@ page import="DBstep.iDBManager2000.*"--%>
<%
   //upload bean
    com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
    //initialization
    mySmartUpload.initialize(pageContext); 
    //set the maximum of upload
    mySmartUpload.setMaxFileSize(500 * 1024*1024);
    //upload file
    mySmartUpload.upload();
   //get all the upload file
   //for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
   //get the upload file
   com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
   
   // get the other information (String)mySmartUpload.getRequest()
    String classid = mySmartUpload.getRequest().getParameter("classid") == null ? "" : (String)mySmartUpload.getRequest().getParameter("classid");
String classs = mySmartUpload.getRequest().getParameter("classs") == null ? "": (String)mySmartUpload.getRequest().getParameter("classs");
String catalog = mySmartUpload.getRequest().getParameter("catalog") == null ? "": (String)mySmartUpload.getRequest().getParameter("catalog");
String descriptions = mySmartUpload.getRequest().getParameter("descriptions") == null ? "": (String)mySmartUpload.getRequest().getParameter("descriptions");
String program = mySmartUpload.getRequest().getParameter("program") == null ? "": (String)mySmartUpload.getRequest().getParameter("program");
String colors = mySmartUpload.getRequest().getParameter("colors") == null ? "": (String)mySmartUpload.getRequest().getParameter("colors");
String views = mySmartUpload.getRequest().getParameter("views") == null ? "" : (String)mySmartUpload.getRequest().getParameter("views");
String myFileName = "";
String fullName = "";
String editor = (String) request.getSession().getAttribute("logUserName");

java.io.File file = null;
java.io.FileInputStream fis = null;
  
   if (!myFile.isMissing())
    {
   //get the name of the upload file
    myFileName = myFile.getFileName();
    fullName = myFile.getFilePathName();

    System.out.println("fullName = " + fullName);
    //get the name the file without suffix
    String  suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
    //get suffix of the file
    String  ext= mySmartUpload.getFiles().getFile(0).getFileExt();  
    //get the size of the file  
    int fileSize=myFile.getSize();
    //store the path
    //String aa=getServletContext().getRealPath("/")+"jsp\\";
    //String trace=aa+myFileName;
    //get other parameters
    //String send=(String)mySmartUpload.getRequest().getParameter("send");
    //store the file in server 
    //myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
    
    //store the upload file in the databank
     
    file = new java.io.File(fullName);
    fis = new java.io.FileInputStream(file);

//   out.println(file.length());
   }
    //open the databank
    Connection con = null; PreparedStatement stmt = null;
Statement stmt2 = null;
try{
String url = "jdbc:mysql://localhost:3306/****?user=****&password=****&useUnicode=true&characterEncoding=8859_1";
con = DriverManager.getConnection(url);

stmt2 = con.createStatement(); ResultSet rs = null;
//-- if the classid is null, then add, otherweise modify
if (classid == null || "".equals(classid)) {
String sql_1 = "select max(classid)+1 as classid2 from graphicslibrary";
rs = stmt2.executeQuery(sql_1);
if (rs.next()) {
classid = String.valueOf(rs.getInt("classid2"));
}
System.out.println("classid ====== "+classid);
if(fis != null){
String sql = " insert into graphicslibrary (classid, class, catalog, descriptions, program, colors, views, picturename, picture, editor, edittime ) "+
  " values (?,?,?,?,?,?,?,?,?,?, now() ) ";
stmt = con.prepareStatement(sql);
stmt.setString(1,classid);
stmt.setString(2,classs);
stmt.setString(3,catalog);
stmt.setString(4,descriptions);
stmt.setString(5,program);
stmt.setString(6,colors);
stmt.setString(7,views);
stmt.setString(8,myFileName);
stmt.setBinaryStream(9,fis,(int)file.length());
stmt.setString(10,editor);
}else{
String sql = " insert into graphicslibrary (classid, class, catalog, descriptions, program, colors, views, editor, edittime ) "+
  " values (?,?,?,?,?,?,?,?, now() ) ";
stmt = con.prepareStatement(sql);
stmt.setString(1,classid);
stmt.setString(2,classs);
stmt.setString(3,catalog);
stmt.setString(4,descriptions);
stmt.setString(5,program);
stmt.setString(6,colors);
stmt.setString(7,views);
stmt.setString(8,editor);
}
System.out.println("+++++++++++++++++++");
stmt.executeUpdate();
}
//-- modify
        .........

解决方案 »

  1.   

    java.io.FileNotFoundException: F:\private\bank\bank\images\bg.jpg (No such file or directory) 
    文件的路径好像不是这么写的,
    F:/private/bank/bank/images/bg.jpg
      

  2.   

    这个路径不是我自己写的,而是通过jsp里浏览,然后找到文件打开,获取的路径信息,所以不知道应该怎么修改程序。另外服务器的系统是linux的
      

  3.   

    另外服务器的系统是linux的见鬼,linux 下面怎么可能有 F:\ 呢!!!代码里根本看不到你的 F:\到底出现在哪里?莫名其妙。
    你现在你的目录里找找看吧。那个 f:\private 到底是哪里来的? 那个 bg.jpg 到底在哪里?然后修改你的 f:\为你的现在的路径
      

  4.   

    服务器是linux,可是用户可以本地访问的,添加记录也是用户操作的,刚才那段错误信息就是我以用户的方式在我的电脑上测试程序的,所以那个F盘下的文件是我自己电脑上的,不是linux主机上的。
      

  5.   

    错误原因好像是上传文件时候服务器无法读取,有另外一个方法读取文件:把java.io.FileInputStream fis = null;
    file = new java.io.File(fullName);
        fis = new java.io.FileInputStream(file);这两句改成com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile();另外如果我想用这个方法修改程序,我应该怎么从对象myFile里获取流的形式?因为下面向数据库里读时候是以流的形式读取这个图片文件的stmt.setBinaryStream(9,fis,(int)file.length()); 
      

  6.   

    你好像在IM里面问过java.io.FileNotFoundException: F:\private\bank\bank\images\bg.jpg (No such file or directory) 你再次确认这个文件真的存在吗?
      

  7.   

    你理解错了,java.io.File只能读取java程序所在的机器的文件,服务器器并没有你选择的文件。
    文件在你自己的电脑里是不能这样写的,
    java上传文件网上有很多例子了,看看就明白了。
    你应该先保存到服务器上,再使用服务器上的文件路径。
      

  8.   

    8楼说的对,我也发现了这个问题,
    java.io.FileInputStream fis = null; 
    file = new java.io.File(fullName); 
    fis = new java.io.FileInputStream(file);
    在5楼说过,但是不知道怎么修改。还有就是如果我先将上传的文件存入linux服务器的C盘根目录下,然后再从服务器上读取文件,这样的话java.io.File就可以用了吧?另外上传到服务器C盘下的图片是在程序结束后就被自动删除掉还是会留在那里?
      

  9.   

    我修改过程序,想法是这样的:先把文件传到服务器端,然后再从服务器读取文件保存到数据库
    //get the name of the upload file
      myFileName = myFile.getFileName();
    //set the store path on the server
      String  trace="/home/"+myFileName; 
    //store the upload file as stream
      file = new java.io.File(trace);
      fis = new java.io.FileInputStream(file);可是新问题出现了,服务器可能存在写入权限,无法保存文件到服务器。
    exception org.apache.jasper.JasperException: An exception occurred processing JSP page /upload.jsp at line 5451:     //get other parameters
    52:     //String send=(String)mySmartUpload.getRequest().getParameter("send");
    53:     //store the file in server 
    54:     myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
    55:     
    56:     //store the upload file as stream
    57:      
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    root cause javax.servlet.ServletException: com.jspsmart.upload.SmartUploadException: File can't be saved (1120).
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.upload_jsp._jspService(upload_jsp.java:236)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    root cause com.jspsmart.upload.SmartUploadException: File can't be saved (1120).
    com.jspsmart.upload.File.saveAs(File.java:108)
    org.apache.jsp.upload_jsp._jspService(upload_jsp.java:110)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.不知道还有没有其他办法,或者我去申请服务器的写入权限?如果申请写入权限,程序路径上应该怎么修改?有没有类似的例程序供参考呢?谢谢能够帮忙的各位了!!!