首先:由Servlet生成:
<html>
<body>
<form method="POST" action="....">
<input type = file name = test>
</form>
</body>
</html>
客户端浏览器选择文件后,提交即可。
servlet处理提交的表单(name = test)即可。

解决方案 »

  1.   

    这个我知道啊, 就是想问, 怎么才能在 servlets 里面把文件从客户端传上去呢?
      

  2.   

    在你的servlet上获取变量test(这你肯定能做到吧!这和一般的text类型的变量是一样的)
    然后将test中的内容写到你想要写的地方即可。
      

  3.   

    客户端html代码
    <form name=Post action=http://www.mycgiserver.com/servlet/javaduke.servlet.FileUpload method=POST enctype="multipart/form-data" ><td width=100% colspan=2 height=6> 
    <input type="file" name="file">
    </td>
    </form>
    servlet代码
    package javaduke.servlet;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;public class FileUpload extends HttpServlet {  //Initialize global variables
      public void init(ServletConfig config) throws ServletException {
        super.init(config);
      }  //Service the request
      public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    String filepath=request.getParameter("file");
        File file=new File(filepath);//错误发生地
        BufferedReader reader=new BufferedReader(new FileReader(file));
        BufferedWriter writer=new BufferedWriter(new FileWriter("/members/RMSmVEcjpXYlu0gRbkJhgHsRnlw72yjR/upload/"+"fileName"));
        int index;
        while((index=reader.read ())!=-1){
          writer.write (index);
        }
        reader.close();
        writer.close();
      }  //Get Servlet information
      public String getServletInfo() {
        return "javaduke.FileUpload Information";
      }

    编译后运行,提示nullpointer错误,可能出在上面我已经标记的地方,但是如何将本地文件上传到服务器上呢? 
    回复贴子: 
    回复人:simoncn(2000-10-3 10:27:00)  得0分 
    你的问题看起来是filepath为空值的原因,解决掉它之后请参考:
    http://www.servlets.com/resources/com.oreilly.servlet/MultipartRequest.html
    下的介绍。
    A utility class to handle multipart/form-data requests, the kind of requests that support file uploads. This class can receive arbitrarily large files (up to an artificial limit you can set), and fairly efficiently too. It cannot handle nested data (multipart content within multipart content) or internationalized content (such as non Latin-1 filenames). 
    It's used like this: MultipartRequest multi = new MultipartRequest(req, ".");
      
    out.println("Params:");
    Enumeration params = multi.getParameterNames();
    while (params.hasMoreElements()) {
      String name = (String)params.nextElement();
      String value = multi.getParameter(name);
      out.println(name + " = " + value);
    }
    out.println();
      
    out.println("Files:");
    Enumeration files = multi.getFileNames();
    while (files.hasMoreElements()) {
      String name = (String)files.nextElement();
      String filename = multi.getFilesystemName(name);
      String type = multi.getContentType(name);
      File f = multi.getFile(name);
      out.println("name: " + name);
      out.println("filename: " + filename);
      out.println("type: " + type);
      if (f != null) {
        out.println("f.toString(): " + f.toString());
        out.println("f.getName(): " + f.getName());
        out.println("f.exists(): " + f.exists());
        out.println("f.length(): " + f.length());
        out.println();
      }
    }A client can upload files using an HTML form with the following structure. Note that not all browsers support file uploads. 
    <FORM ACTION="/servlet/Handler" METHOD=POST
              ENCTYPE="multipart/form-data">
    What is your name? <INPUT TYPE=TEXT NAME=submitter> <BR>
    Which file to upload? <INPUT TYPE=FILE NAME=file> <BR>
    <INPUT TYPE=SUBMIT>
    </FORM>The full file upload specification is contained in experimental RFC 1867, available at http://www.ietf.org/rfc/rfc1867.txt. 
     
      

  4.   

    去下载jspSmartupload组件吧,很不错的!