有人实际操作过doPUT吗?刚尝试使用PUT来上传文件,可是即使html中的表单method设置为put,到了容器中的servlet会转换到doGet方法。
程序如下:
html页面:
<html>
<body>
<form enctype="multipart/form-data" method="PUT"
action="http://localhost:8080/textjsp/welcome/testdefault">
<input type="file" size="20" name="FileToUpload"
value="Select File">
<input type="submit" name="UploadFile" value="Upload">
<input type="reset" value="Reset">
</form>
</body>
</html>servlet如下:
import java.io.*;
import java.util.*; 
import java.net.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class UploadServlet extends HttpServlet { /**
 * Constructor of the object.
 */
public UploadServlet() {
super();
} /**
 * Destruction of the servlet. <br>
 */
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

static final String dir="C:/temp";
    
    public void doPut(HttpServletRequest req, 
    HttpServletResponse res)
    throws ServletException, IOException
    {
     PrintWriter outHTML = res.getWriter();
     outHTML.println("done enter doPut");
      /*  
     try 
     {
        int i;
        InputStream input;
        input = req.getInputStream();
        BufferedInputStream in = 
           new BufferedInputStream(input);
        BufferedReader reader = 
           new BufferedReader(
             new InputStreamReader(in));
        File outputFile = 
           new File("c:/temp/out.txt");
        FileWriter out = 
           new FileWriter(outputFile);
    
        while ((i = reader.read()) != -1) 
        {
          out.write(i);
        }
    
        out.close();
        in.close();
    
      }
      catch (IOException e) {}*/
    }


/**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
// Put your code here
}}