有没有那本书讲过如何用java实现文件上传下载啊?

解决方案 »

  1.   

    好象挺多的,如 JSP2.0技术手册   某些案例教程  都有吧
      

  2.   

    commons-io-1.4-bin.zip+commons-fileupload-1.2.1-bin.zip
      

  3.   

    import java.io.FileInputStream;
    import java.io.OutputStream;import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class FileDownload extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
    try {
    String fname = "test.xls";
    response.setCharacterEncoding("UTF-8");
    fname = java.net.URLEncoder.encode(fname, "UTF-8");
    response.setHeader("Content-Disposition", "attachment; filename="+fname);
    response.setContentType("application/msexcel");// 定义输出类型
    } catch (Exception e) {
    System.out.println(e);
    }
    }
    }
    //上面是下载用的servlet
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Iterator;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.FileItemFactory;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;public class FileUpload extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    Iterator items;
    try {
    items = upload.parseRequest(request).iterator();
    while (items.hasNext()) {
    FileItem item = (FileItem) items.next();
    if (!item.isFormField()) {
    //取出上传文件的文件名称
        String name = item.getName();    
        String fileName = name.substring(name.lastIndexOf('\\')+1,name.length());
        String path = request.getRealPath("file")+File.separatorChar+fileName;
        //上传文件
        File uploadedFile = new File(path);
        item.write(uploadedFile);
        
        //打印上传成功信息
        response.setContentType("text/html");
    response.setCharacterEncoding("gb2312");
        PrintWriter out = response.getWriter();
        out.print("<font size='2'>上传的文件为:"+name+"<br>");
        out.print("保存的地址为:"+path+"</font>");
    }
    }
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    // 上面是 上传的servlet希望能给楼主一点帮助,我和你一样  也是在学习中
      

  4.   

    个人觉得fileupload的上传方式不怎么好,如果有上传文件名是中文,还必须得把编码方式设置成UTF-8,况且这个时候像取除上传之外的参数,用getParameter()取不到,还得用FileItem.getString("UTF-8");很麻烦。建议用struts自带的上传方法,很简单的..
    ---------------------------------------------------
    Quietly through ....
      

  5.   

    可以试试jspSmartUpload组件,很好用,很方便
      

  6.   

    文件上传类:MoqUploadBean.java     
      package   net.moq.www;   
      import   java.io.*;   
      import   java.util.*;   
      import   javax.servlet.*;   
      import   javax.servlet.http.*;   
      /**   
        *   
        *   Title:   文件上传类   
        *   Description:   既能对文件进行上传,又能取得输入框的值,最多可同时上传255个文件   
        *   Copyright:   Copyright   (c)   2002   
        *   Company:   Tekson   
        *   @author   莫琼   
        *   @version   1.0   
        */   
      public   class   MoqUploadBean   {   
          private   String[]   sourceFile   =   new   String[255];           //源文件名   
          private   String[]   suffix   =   new   String[255];                   //文件后缀名   
          private   String   canSuffix   =   ".gif.jpg.jpeg.png";         //可上传的文件后缀名   
          private   String   objectPath   =   "c:/";                                   //目标文件目录   
          private   String[]   objectFileName   =   new   String[255];   //目标文件名   
          private   ServletInputStream   sis   =   null;                           //输入流   
          private   String[]   description   =   new   String[255];         //描述状态   
          private   long   size   =   100   *   1024;                                         //限制大小   
          private   int   count   =   0;                                                           //已传输文件数目   
          private   byte[]   b   =   new   byte[4096];                                   //字节流存放数组   
          private   boolean   successful   =   true;   
          private   Hashtable   fields   =   new   Hashtable();   
          public   MoqUploadBean()   {   
          }   
          //设置上传文件的后缀名   
          public   void   setSuffix(String   canSuffix)   {   
              this.canSuffix   =   canSuffix;   
          }   
          //设置文件保存路径   
          public   void   setObjectPath(String   objectPath)   {   
              this.objectPath   =   objectPath;   
          }   
          //设置文件保存路径   
          public   void   setSize(long   maxSize)   {   
              this.size   =   maxSize;   
          }   
          //文件上传处理程序   
          public   void   setSourceFile(HttpServletRequest   request)   throws   IOException   {   
              sis   =   request.getInputStream();   
              int   a   =   0;   
              int   k   =   0;   
              String   s   =   "";   
              while   (   (a   =   sis.readLine(b,   0,   b.length))   !=   -1)   {   
                  s   =   new   String(b,   0,   a);   
                  if   (   (k   =   s.indexOf("filename=\""))   !=   -1)   {   
                      //   取得文件数据   
                      s   =   s.substring(k   +   10);   
                      k   =   s.indexOf("\"");   
                      s   =   s.substring(0,   k);   
                      sourceFile[count]   =   s;   
                      k   =   s.lastIndexOf(".");   
                      suffix[count]   =   s.substring(k   +   1);   
                      if   (canTransfer(count))   {   
                          transferFile(count);   
                      }   
                      ++count;   
                  }   else   if   (   (k   =   s.indexOf("name=\""))   !=   -1)   {   
                      //   普通表单输入元素,获取输入元素名字   
                      String   fieldName   =   s.substring(k+6,   s.length()-3);   
                      sis.readLine(b,   0,   b.length);   
                      StringBuffer   fieldValue   =   new   StringBuffer(b.length);   
                      while   (   (a   =   sis.readLine(b,   0,   b.length))   !=   -1)   {   
                          s   =   new   String(b,   0,   a-2);   
                          if   (   (b[0]   ==   45)   &&   (b[1]   ==   45)   &&   (b[2]   ==   45)   &&   (b[3]   ==   45)   &&   (b[4]   ==   45))   {   
                              break;   
                          }   else   {   
                              fieldValue.append(s);   
                          }   
                      }   
                      fields.put(fieldName,   fieldValue.toString());   
                  }   
                  if   (!successful)   
                      break;   
              }   
          }   
          //取得表单元素值   
          public   String   getFieldValue(String   fieldName)   {   
              if   (fields   ==   null   ||   fieldName   ==   null)   {   
                  return   null;   
              }   
              return   (String)   fields.get(fieldName);   
          }   
          //取得上传文件数   
          public   int   getCount()   {   
              return   count;   
          }   
          //取得目标路径   
          public   String   getObjectPath()   {   
              return   objectPath;   
          }   
          //取得源文件名   
          public   String[]   getSourceFile()   {   
              return   sourceFile;   
          }   
          //取得目标文件名   
          public   String[]   getObjectFileName()   {   
              return   objectFileName;   
          }   
          //取得上传状态描述   
          public   String[]   getDescription()   {   
              return   description;   
          }   
          //判断上传文件的类型   
          private   boolean   canTransfer(int   i)   {   
              suffix[i]   =   suffix[i].toLowerCase();   
              //这个是用来传图片的,各位可以把后缀名改掉或者不要这个条件   
              if   (sourceFile[i].equals("")   ||   (!(canSuffix.indexOf("."+suffix[i])>=0)))   {   
                  description[i]   =   "ERR:   File   suffix   is   wrong.";   
                  return   false;   
              }   
              else   {   
                  return   true;   
              }   
          }   
          //上传文件转换   
          private   void   transferFile(int   i)   {   
              String   x   =   Long.toString(new   java.util.Date().getTime());   
              try   {   
                  objectFileName[i]   =   x   +   "."   +   suffix[i];   
                  FileOutputStream   out   =   new   FileOutputStream(objectPath   +   objectFileName[i]);   
                  int   a   =   0;   
                  int   k   =   0;   
                  long   hastransfered   =   0;   //标示已经传输的字节数   
                  String   s   =   "";   
                  while   (   (a   =   sis.readLine(b,   0,   b.length))   !=   -1)   {   
                      s   =   new   String(b,   0,   a);   
                      if   (   (k   =   s.indexOf("Content-Type:"))   !=   -1)   {   
                          break;   
                      }   
                  }   
                  sis.readLine(b,   0,   b.length);   
                  while   (   (a   =   sis.readLine(b,   0,   b.length))   !=   -1)   {   
                      s   =   new   String(b,   0,   a);   
                      if   (   (b[0]   ==   45)   &&   (b[1]   ==   45)   &&   (b[2]   ==   45)   &&   (b[3]   ==   45)   &&   (b[4]   ==   45))   {   
                          break;   
                      }   
                      out.write(b,   0,   a);   
                      hastransfered   +=   a;   
                      if   (hastransfered   >=   size)   {   
                          description[count]   =   "ERR:   The   file   "   +   sourceFile[count]   +   
                                  "   is   too   large   to   transfer.   The   whole   process   is   interrupted.";   
                          successful   =   false;   
                          break;   
                      }   
                  }   
                  if   (successful)   {   
                      description[count]   =   "Right:   The   file   "   +   sourceFile[count]   +   
                              "   has   been   transfered   successfully.";   
                  }   
                  out.close();   
                  if   (!successful)   {   
                      sis.close();   
                      File   tmp   =   new   File(objectPath   +   objectFileName[count]);   
                      tmp.delete();   
                  }   
              }   
              catch   (IOException   ioe)   {   
                  description[i]   =   ioe.toString();   
              }   
          }   
          public   static   void   main(String[]   args)   {   
              System.out.println("Test   OK");   
          }   
      }   
      

  7.   

      文件上传提交:MoqUploadSubmit.jsp   
      〈%@   page   contentType="text/html;charset=gb2312"%>   
      〈jsp:useBean   id="fileBean"   scope="page"   class="net.moq.www.MoqUploadBean"   />   
      〈%   
      fileBean.setObjectPath("D:\\Temp\\");   
      fileBean.setSize(10000*1024);   
      fileBean.setSuffix(".gif.jpg.png.jpge.html.htm");   
      fileBean.setSourceFile(request);   
      String   []   saSourceFile   =   fileBean.getSourceFile();   
      String   []   saObjectFile   =   fileBean.getObjectFileName();   
      String   []   saDescription   =   fileBean.getDescription();   
      int   iCount   =   fileBean.getCount();   
      String   sObjectPath   =   fileBean.getObjectPath();   
      for(int   i=0;i〈iCount;i++)   {   
          out.println("〈br>源始文件:");   
          out.println(saSourceFile[i]);   
          out.println("〈br>目标文件:");   
          out.println(sObjectPath+saObjectFile[i]);   
          out.println("〈br>上传说明:");   
          out.println(saDescription[i]);   
          out.println("〈br>");   
      }   
      out.println("〈br>作者:"   +   fileBean.getFieldValue("Author"));   
      out.println("〈br>公司:"   +   fileBean.getFieldValue("Company"));   
      out.println("〈br>说明:"   +   fileBean.getFieldValue("Comment"));   
      %>  
      

  8.   

    xvwei111 我想知道怎么用 谢谢 我是新手 谢谢了
      

  9.   

    xvwei111  真是坑啊,根本就不可以用