请问这段代码,路径哪里设置错了 ?
package pageconnection;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;public class UploadFile2 {
        private UploadFile2() {
    }
    
        private static String getFileType(String fileName) {
        int index = -1;
        index = fileName.lastIndexOf(".");
        if (index>0) {
            return fileName.substring(index,fileName.length());
        } else {
            return "."+fileName.substring(fileName.length()-3,fileName.length());
        }
    }
        public static String[] upload(HttpServletRequest request,String url,String fileName) 
    throws ServletException,IOException {
        return upload(request,url,fileName,false);
    }
        public static String[] upload(HttpServletRequest request,String url,boolean useAutogenerationName)
    throws ServletException,IOException {
        return upload(request,url,null,useAutogenerationName);
    }
       @SuppressWarnings("unchecked")
private static String[] upload(HttpServletRequest request,String url,String fileName,boolean useAutogenerationName) 
    throws ServletException,IOException {
        String name = null; 
        String value = null; 
        boolean fileFlag = false;  
        File tmpFile = null; 
        String fName = null; 
        FileOutputStream baos = null; 
        BufferedOutputStream bos = null; 
        Hashtable<String, ArrayList<String>> paramHt = new Hashtable<String, ArrayList<String>>(); 
        int BUFSIZE = 1024 * 8; 
        int rtnPos = 0; 
        byte[] buffs = new byte[BUFSIZE * 8]; 
        String contentType = request.getContentType();
        request.getInputStream() ;
        int index = contentType.indexOf("boundary="); 
        String boundary = "--" + contentType.substring(index + 9); 
        String endBoundary = boundary + "--";
        ServletInputStream sis = null;
        try {
            sis = request.getInputStream();
        } catch (IOException e){
            System.out.println("��ȡServlet�ֽ����?");
           e.printStackTrace();
            throw e;
        }
        try {
            while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) { 
                String strBuff = new String(buffs, 0, rtnPos); 
                if (strBuff.startsWith(boundary)) { 
                    if (name != null && name.trim().length() > 0) { 
                        if (fileFlag) { 
                            bos.flush(); 
                            baos.close(); 
                            bos.close(); 
                            baos = null; 
                            bos = null; 
                        } else { 
                            Object obj = paramHt.get(name); 
                            ArrayList<String> al = null; 
                            if (obj == null) { 
                                al = new ArrayList<String>(); 
                            } else { 
                                ArrayList arrayList = (ArrayList)obj; 
                                al = arrayList; 
                            } 
                            al.add(value); 
                            paramHt.put(name, al); 
                        } 
                    } 
                    name = new String(); 
                    value = new String(); 
                    fileFlag = false; 
                    rtnPos = sis.readLine(buffs, 0, buffs.length);
                    if (rtnPos != -1) { 
                        strBuff = new String(buffs, 0, rtnPos);
                        if (strBuff.toLowerCase().startsWith("content-disposition: form-data; ")) { 
                            int nIndex = strBuff.toLowerCase().indexOf("name=\""); 
                            int nLastIndex = strBuff.toLowerCase().indexOf("\"",nIndex + 6); 
                            name = strBuff.substring(nIndex + 6, nLastIndex); 
                        }
                        int fIndex = strBuff.toLowerCase().indexOf("filename=\""); 
                        if (fIndex != -1) { 
                            fileFlag = true; 
                            int fLastIndex = strBuff.toLowerCase().indexOf("\"",fIndex + 10); 
                            fName = strBuff.substring(fIndex + 10, fLastIndex);
                            fIndex = fName.lastIndexOf("\\"); 
                            if (fIndex == -1) { 
                                fIndex = fName.lastIndexOf("/"); 
                                if (fIndex != -1) { 
                                    fName = fName.substring(fIndex + 1); 
                                } 
                            } else { 
                                fName = fName.substring(fIndex + 1); 
                            } 
                            if (fName == null || fName.trim().length() == 0) { 
                                fileFlag = false; 
                                sis.readLine(buffs, 0, buffs.length); 
                                sis.readLine(buffs, 0, buffs.length); 
                                sis.readLine(buffs, 0, buffs.length); 
                                continue; 
                            } 
                        } 
                        sis.readLine(buffs, 0, buffs.length); 
                        sis.readLine(buffs, 0, buffs.length); 
                    } 
                } else if (strBuff.startsWith(endBoundary)) { 
                    if (name != null && name.trim().length() > 0) { 
                        if (fileFlag) { 
                            bos.flush(); 
                            baos.close(); 
                            bos.close(); 
                            baos = null; 
                            bos = null; 
                        } else { 
                            Object obj = paramHt.get(name); 
                            ArrayList<String> al = null; 
                            if (obj == null) { 
                                al = new ArrayList<String>(); 
                            } else { 
                                ArrayList<String> arrayList = (ArrayList<String>) obj; 
                                al = arrayList; 
                            } 
                            al.add(value); 
                            paramHt.put(name, al); 
                        } 
                    } 
                } else { 
                    if (fileFlag) { 
                        if (baos == null && bos == null) {
                            if (useAutogenerationName == true) {
                                fName = System.currentTimeMillis()+getFileType(fName);
                            } else {
                                if (fileName != null) {
                                    fName = fileName+getFileType(fName);
                                }
                            }
                            tmpFile = new File(url + fName); 
                            baos = new FileOutputStream(tmpFile); 
                            bos = new BufferedOutputStream(baos); 
                        } 
                        bos.write(buffs, 0, rtnPos); 
                        baos.flush(); 
                    } else { 
                        value = value + strBuff; 
                    } 
                } 
            } 
        } catch (IOException e) {
            System.out.println("д���ļ��쳣�����ļ����ȡ����");
            e.printStackTrace();
            throw e;
        }
        try {
            if (bos != null ) {
                bos.close();
            }
        } catch (IOException e) {
         System.out.println("�ر�д���ֽ����?");
            e.printStackTrace();
            throw e;
        }
        if (tmpFile != null) {
            //return url + fName;
            String message[] = {null,null};
            File file = new File(url + fName);
            message[0] = fName;
            message[1] = getFileSize(file.length());
            return message;
        } else {
            return null;
        }
    }
       protected static String getFileSize(long size) {
        StringBuffer sb = new StringBuffer(String.valueOf(size));
        int length = sb.length();
        if (sb.length()<4) {
            sb.append("B");
        } else if (sb.length()<7) {
            sb = new StringBuffer(sb.substring(0, length-3)+"."+sb.substring(length-3,length-2)+"K");
        } else if (sb.length()<10) {
            sb= new StringBuffer(sb.substring(0, length-6)+"."+sb.substring(length-6,length-5)+"M");
        } else {
            sb= new StringBuffer(sb.substring(0, length-9)+"."+sb.substring(length-9,length-8)+"G");
        }
        return sb.toString();
    }
}

解决方案 »

  1.   

    上面的是Upload.java
    下面这个是UploadServlet.java,这两个是放在 Eclipse的src中的,见截图:package pageconnection;import java.io.IOException;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.DiskFileUpload;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;public class UploadServlet2 extends HttpServlet{

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
    System.out.println("Success!");

    String select  = request.getParameter("select1") ;
    String name    = request.getParameter("name") ;
     
    String ss[] = 
    UploadFile2.upload(request, select, false);


    String filePath  = request.getParameter("fileData") ; 

    System.out.println(filePath) ;

    System.out.println("SS:" + ss[0] +"  " +ss[1]) ;
    }
    }
      

  2.   

    因为我现在的问题是:图片能够上传,但是无法显示在页面上,所以是路径问题,想请问,路径该怎么改,是在upload.java里面改吧?!
      

  3.   

    信息: Server startup in 3344 ms
    new_jididongtai.html
    C:\Documents and Settings\All Users\Documents\My Pictures\示例图片\Water lilies.jpg
    Water lilies.jpg
    hi
     insert into images(name ,title ,path ,time) values('new_jididongtai.htmlWater lilies.jpg','hi','F:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\page\image','2009-8-26 20:55:58')
    Duplicate entry 'new_jididongtai.htmlWater lilies.jpg' for key 1
    jididongtaiServlet
    127.0.0.1//D:JAVATomcat5.5webappspageimage/--è??????????????????±????--7.jpg
    127.0.0.1//F:workspace.metadata.pluginsorg.eclipse.wst.server.core mp0webappspageimage/new_deguomuyangquan.htmlWater lilies.jpg
    127.0.0.1//F:workspace.metadata.pluginsorg.eclipse.wst.server.core mp0webappspageimage/new_jididongtai.htmlWater lilies.jpg
    127.0.0.1//F:workspace.metadata.pluginsorg.eclipse.wst.server.core mp0webappspageimage/new_youao.htmlBlue hills.jpg
    127.0.0.1//F:workspace.metadata.pluginsorg.eclipse.wst.server.core mp0webappspage2image/new_zangao.html27.jpg
    这个是异常信息
      

  4.   

    package pageconnection;import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import com.jdbc.Jdbc;public class JididongtaiServlet extends HttpServlet{

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
         throws ServletException, IOException {


    Jdbc jdbc = new Jdbc() ;
    List list = jdbc.getLatestImages() ; //JDBC

    request .setAttribute("latestImages", list) ;
    System.out.println("jididongtaiServlet") ;

     request.getRequestDispatcher("/pages/jididongtai.jsp").forward(request, response);

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
      doPost(request, response) ;
    }
    }这段是JididongtaiServlet.java 的代码,想请问,是修改request .setAttribute("latestImages", list) ;这里面的?!初学,请指教!