我自己写了一个,但注意,在有些系统上可能不支持中文路径: 如果你想再传其它参数,应该把参数手动拼到URL后面如:form1.action=form1.action+"?arg1="+form1.name.value; upload.jsp代码: <%@ page language="java" pageEncoding="GBK"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Insert title here</title> </head> <body> <form name="form1" action="/upload/UploadServlet" method="POST" ENCTYPE="multipart/form-data"> <input type="file" name="file1"/><br> 复核人:<input name="verify" value="12312312312"/><input name="verify1" value="12312312312"/><br> <input type="submit"value="上传"/> </form> </body> </html> UploadServlet 代码: package servlet;import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import save.SaveFile;public class UploadServlet extends HttpServlet { private static final String LINE_SEPARATOR ="\r\n"; /** * 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 { doPost(request,response); } /** * 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 { String contentType = request.getContentType(); InputStream in1=request.getInputStream(); int offset = -1; StringBuffer sb=new StringBuffer(); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\abc.zip")); byte[] b=new byte[4096]; while((offset=in1.read(b,0,4096))>-1){ sb.append(new String(b,0,offset,"UTF-8")); bos.write(b, 0, offset); } bos.flush(); bos.close(); } public void init() throws ServletException { // Put your code here }}
网上很多例子的
如果你想再传其它参数,应该把参数手动拼到URL后面如:form1.action=form1.action+"?arg1="+form1.name.value;
upload.jsp代码:
<%@ page language="java"
pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form name="form1" action="/upload/UploadServlet" method="POST" ENCTYPE="multipart/form-data">
<input type="file" name="file1"/><br>
复核人:<input name="verify" value="12312312312"/><input name="verify1" value="12312312312"/><br>
<input type="submit"value="上传"/>
</form>
</body>
</html>
UploadServlet 代码:
package servlet;import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import save.SaveFile;public class UploadServlet extends HttpServlet {
private static final String LINE_SEPARATOR ="\r\n"; /**
* 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 { doPost(request,response);
} /**
* 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 {
String contentType = request.getContentType();
InputStream in1=request.getInputStream();
int offset = -1;
StringBuffer sb=new StringBuffer();
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\abc.zip"));
byte[] b=new byte[4096];
while((offset=in1.read(b,0,4096))>-1){
sb.append(new String(b,0,offset,"UTF-8"));
bos.write(b, 0, offset);
}
bos.flush();
bos.close(); }
public void init() throws ServletException {
// Put your code here
}}