用 fileinputstream 类将文件读作byte流,然后就可以写到数据库!

解决方案 »

  1.   

    byte[] aBtytes;
    FileInputStream afileinputstream=new FileInputStream(filepath);
    afileinputstream.read(aBtytes);
    afileinoutstream.close();
    aBtytes里就是文件!
      

  2.   

    那你要取到服务器的路径!
    放到filepath里面!
      

  3.   

    http://www.jspsmart.com/
      网页左侧有个链接--jspSmartUpload,这是一个免费的文件上传的JavaBean,很多人在用。你可以下载该bean,并参考它提供的例子。另外,可以参考:http://www-900.ibm.com/developerWorks/java/fileup/index.shtml
      

  4.   

    用jspsmartupload上传文件时
    String upfileroot=request.getRemoteAddr(); 
    int port=request.getServerPort();
    path="http://"+upfileroot+":"+String.valueOf(port)+"/doc/";filename=nf.getFileName();
    用nf.saveAs(upfileroot+filename);
    为什么说路径错误呢???
      

  5.   

    什么文件都能上传!!!
    public class ImportServlet extends HttpServlet {
      private static final String CONTENT_TYPE = "text/html; charset=GBK";
      private static final int ONE_MEG = 2048*1024;  //Initialize global variables
      public void init() throws ServletException {
      }  //Process the HTTP Get request
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
      }
      //Process the HTTP Post request
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int buffsize = 1024*8;
        int contentLength = request.getContentLength();
        // Hashtable pageItems = null;
        // value chosen to limit denial of service - 2 MB, in this case
        if (contentLength > ONE_MEG){
          System.out.println ("contentLength > ONE_MEG");
          response.setContentType("text/html");
          ServletOutputStream out = response.getOutputStream();
          out.println("<html><head><title>Too big</title></head>");
          out.println("<body><h1>错误 - 文件大于2M!");
          out.println("</h1></body></html>");
          out.close();
        }
        else{
          HttpSession session = request.getSession(true);
          int status=0;//status=0:非正文;status=1:分隔符;status=2:文件内容;status=3:其它内容
          String ContentType=request.getHeader("content-type");
          String boundary=ContentType.substring(ContentType.indexOf("boundary=")+9);
          String fileName="";
          String itemName="";
          String lastItem="";
          Properties pp = new Properties();
          try{
            pp = configInfo.getConfigObj();
          }catch(Exception e){      }      String strPath=pp.getProperty("OutputPath");// "D:\\Application_Studio\\JTP\\lib\\";
          ServletInputStream in = request.getInputStream();
          byte[] b = new byte[buffsize];
          byte[] byContent = new byte[contentLength];
          int result;
          int totalRead = 0;
          FileOutputStream fileout = null;
          try{
            result = in.readLine(b, 0, b.length);
            while (result != -1){
              String rowStr=new String(b,0,result);
              if (IsBoundary(rowStr,boundary)){ //遇到分割符
                switch(status){
                  case 0:break;
                  case 1:break;
                  case 2:
                    if ( totalRead!=0&!fileName.equals("")){
                      fileout =new FileOutputStream(strPath+fileName);
                      fileout.write(byContent, 0, totalRead);
                      totalRead=0 ;
                    }
                    break;
                  case 3:
                    String itemContent=new String(byContent,0,totalRead);
                    if (itemName.equals(lastItem)){
                      String sval =(String) session.getAttribute (itemName);
                      sval+=";"+ itemContent;
                      session.setAttribute(itemName, sval);
                    }
                    else{
                      session.setAttribute(itemName, itemContent);
                    }
                    lastItem=itemName;
                    break;
                }
                totalRead=0;
                status=0;
              }
              else if ( status<2 ){ //非正文内容
                if (IsFile(rowStr)){
                  fileName=getFilename(rowStr);
                  if (!fileName.equals("")){
                    session.setAttribute("filename", "<A href=http://192.168.66.87:8080/upload/"+ fileName+">"+ fileName+"</A>");
                  }              result = in.readLine(b, 0, buffsize);//跳过冗余信息
                  result = in.readLine(b, 0, buffsize);//跳过冗余信息
                  status=2;
                }
                else if (IsElement(rowStr)){
                  itemName=getElename(rowStr);
                  status=3;
                }
              }
              else {   //上载文件内容或表单提交的数据
                for ( int i=0; i<result; i++ ){
                  byContent[totalRead+i]=b[i];
                }
                totalRead+=result;
              }          result = in.readLine(b, 0, buffsize);        }
            fileout.close();
          }catch (IOException ioe){
            System.out.println("IOException at Import File:"+ioe.getMessage());
            ioe.printStackTrace();
          }
        }//end if (contentLength > ONE_MEG)
        response.setContentType("text/html");
        PrintWriter out = new PrintWriter (response.getOutputStream());
        out.println("<html>");
        out.println("<head><title>upload</title></head>");
        out.println("<body>");
        out.println("文件上传成功!");
        out.println("</body></html>");
        out.close();
      }//end doPost()  public String getServletInfo(){
        return "A servlet that shows the request headers and body sent by the client";
      }
      public boolean IsBoundary(String str,String boundary){
        if(str.length() < 40 & str.length() > 60 ) return false;
        if(str.indexOf(boundary)!=-1) return true;
        else return false;
      }
      public boolean IsFile(String str){
        if(str.length() < 53 & str.length() >253) return false;
        if(str.indexOf("filename=")!=-1) return true;
        else return false;
      }
      public boolean IsElement(String str){
        if(str.length() < 40 & str.length() >253) return false;
        if(str.indexOf("name=")!=-1) return true;
        else return false;
      }
      public String getFilename(String str){
        String pathName=str.substring(str.indexOf("filename="));
        int startIndex=pathName.lastIndexOf("\\")+1;
        int endIndex=pathName.length()-3;
        return pathName.substring(startIndex,endIndex);
      }
      public String getElename(String str){
        String tempStr=str.substring(str.indexOf("name=\"")+6);
        return tempStr.substring(0,tempStr.indexOf("\""));
      }  //Clean up resources
      public void destroy() {
      }
    }
      

  6.   

    什么都能上传
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import com.neusoft.ExtendLibraries.ExtendConfig.configInfo;public class ImportServlet extends HttpServlet {
      private static final String CONTENT_TYPE = "text/html; charset=GBK";
      private static final int ONE_MEG = 2048*1024;  //Initialize global variables
      public void init() throws ServletException {
      }  //Process the HTTP Get request
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
      }
      //Process the HTTP Post request
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int buffsize = 1024*8;
        int contentLength = request.getContentLength();
        // Hashtable pageItems = null;
        // value chosen to limit denial of service - 2 MB, in this case
        if (contentLength > ONE_MEG){
          System.out.println ("contentLength > ONE_MEG");
          response.setContentType("text/html");
          ServletOutputStream out = response.getOutputStream();
          out.println("<html><head><title>Too big</title></head>");
          out.println("<body><h1>错误 - 文件大于2M!");
          out.println("</h1></body></html>");
          out.close();
        }
        else{
          HttpSession session = request.getSession(true);
          int status=0;//status=0:非正文;status=1:分隔符;status=2:文件内容;status=3:其它内容
          String ContentType=request.getHeader("content-type");
          String boundary=ContentType.substring(ContentType.indexOf("boundary=")+9);
          String fileName="";
          String itemName="";
          String lastItem="";
          Properties pp = new Properties();
          try{
            pp = configInfo.getConfigObj();
          }catch(Exception e){      }      String strPath=pp.getProperty("OutputPath");// "D:\\Application_Studio\\JTP\\lib\\";
          ServletInputStream in = request.getInputStream();
          byte[] b = new byte[buffsize];
          byte[] byContent = new byte[contentLength];
          int result;
          int totalRead = 0;
          FileOutputStream fileout = null;
          try{
            result = in.readLine(b, 0, b.length);
            while (result != -1){
              String rowStr=new String(b,0,result);
              if (IsBoundary(rowStr,boundary)){ //遇到分割符
                switch(status){
                  case 0:break;
                  case 1:break;
                  case 2:
                    if ( totalRead!=0&!fileName.equals("")){
                      fileout =new FileOutputStream(strPath+fileName);
                      fileout.write(byContent, 0, totalRead);
                      totalRead=0 ;
                    }
                    break;
                  case 3:
                    String itemContent=new String(byContent,0,totalRead);
                    if (itemName.equals(lastItem)){
                      String sval =(String) session.getAttribute (itemName);
                      sval+=";"+ itemContent;
                      session.setAttribute(itemName, sval);
                    }
                    else{
                      session.setAttribute(itemName, itemContent);
                    }
                    lastItem=itemName;
                    break;
                }
                totalRead=0;
                status=0;
              }
              else if ( status<2 ){ //非正文内容
                if (IsFile(rowStr)){
                  fileName=getFilename(rowStr);
                  if (!fileName.equals("")){
                    session.setAttribute("filename", "<A href=http://192.168.66.87:8080/upload/"+ fileName+">"+ fileName+"</A>");
                  }              result = in.readLine(b, 0, buffsize);//跳过冗余信息
                  result = in.readLine(b, 0, buffsize);//跳过冗余信息
                  status=2;
                }
                else if (IsElement(rowStr)){
                  itemName=getElename(rowStr);
                  status=3;
                }
              }
              else {   //上载文件内容或表单提交的数据
                for ( int i=0; i<result; i++ ){
                  byContent[totalRead+i]=b[i];
                }
                totalRead+=result;
              }          result = in.readLine(b, 0, buffsize);        }
            fileout.close();
          }catch (IOException ioe){
            System.out.println("IOException at Import File:"+ioe.getMessage());
            ioe.printStackTrace();
          }
        }//end if (contentLength > ONE_MEG)
        response.setContentType("text/html");
        PrintWriter out = new PrintWriter (response.getOutputStream());
        out.println("<html>");
        out.println("<head><title>upload</title></head>");
        out.println("<body>");
        out.println("文件上传成功!");
        out.println("</body></html>");
        out.close();
      }//end doPost()  public String getServletInfo(){
        return "A servlet that shows the request headers and body sent by the client";
      }
      public boolean IsBoundary(String str,String boundary){
        if(str.length() < 40 & str.length() > 60 ) return false;
        if(str.indexOf(boundary)!=-1) return true;
        else return false;
      }
      public boolean IsFile(String str){
        if(str.length() < 53 & str.length() >253) return false;
        if(str.indexOf("filename=")!=-1) return true;
        else return false;
      }
      public boolean IsElement(String str){
        if(str.length() < 40 & str.length() >253) return false;
        if(str.indexOf("name=")!=-1) return true;
        else return false;
      }
      public String getFilename(String str){
        String pathName=str.substring(str.indexOf("filename="));
        int startIndex=pathName.lastIndexOf("\\")+1;
        int endIndex=pathName.length()-3;
        return pathName.substring(startIndex,endIndex);
      }
      public String getElename(String str){
        String tempStr=str.substring(str.indexOf("name=\"")+6);
        return tempStr.substring(0,tempStr.indexOf("\""));
      }  //Clean up resources
      public void destroy() {
      }
    }