你不要用form.action的方法,应该用hidden字段
<form action="servlet" method="post|get">
<input type="hidden" name="china" value="中文">
...
这样如果需要的话,还可以在js中修改china的值
<form action="servlet" method="post|get">
<input type="hidden" name="china" value="中文">
...
这样如果需要的话,还可以在js中修改china的值
doPost是body传值。
要上传文件只能用doPost。
/**
* <p>Title: UploadFileServlet</p>
* <p>Description: Upload a file to the serve</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: shadow</p>
* @author shadow
* @version 1.0 final
*/
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletException;public class UploadFileServlet extends HttpServlet { private ServletInputStream sin = null;
private OutputStream fout = null;
private String CharacterEncoding = "";
private String ContentType = "";
private String filename = ""; private byte temp[] = new byte[4096]; protected static final String newline = "\n";
protected static final String UploadDir = "./config/mydomain/applications/DefaultWebApp"; protected final void doPost(HttpServletRequest request, HttpServletResponse response) {
System.out.println("************************************");
upload(request, response);
} protected final void doGet(HttpServletRequest request, HttpServletResponse response) {
System.out.println("************************************");
System.out.println("request.getMethod():" + request.getMethod());
// upload(request, response);
System.out.println(request.getMethod() + " CAN NOT UPLOAD A FILE");
} public void upload(HttpServletRequest request, HttpServletResponse response) { String ContentType = "";
int i = 0; setCharacterEncoding(request.getCharacterEncoding());
System.out.println("request.getMethod():" + request.getMethod());
System.out.println("request.getContentType():" + request.getContentType());
System.out.println("ok");
setContentType(request.getContentType()); try {
sin = request.getInputStream();
filename = getFileName(sin);
//**************
i = sin.readLine(temp, 0, temp.length);
if (this.CharacterEncoding != null) {
ContentType = new String(temp, 0, i, this.CharacterEncoding);
}
else {
ContentType = new String(temp, 0, i);
}
System.out.println("ContentType:" + ContentType);
if (ContentType.indexOf("Content-Type") >= 0) {
System.out.println("uploading ...");
sin.readLine(temp, 0, temp.length);
}
//**************
if ((filename != null) || (!filename.equals(""))) {
fout = new FileOutputStream(new File(UploadDir, filename)); while ((i = sin.readLine(temp, 0, temp.length)) != -1) { if (this.CharacterEncoding != null) {
ContentType = new String(temp, 0, i, this.CharacterEncoding);
}
else {
ContentType = new String(temp, 0, i);
}
if ((ContentType.indexOf(this.ContentType) == 0)&&(temp[0] == 45)) {
System.out.println("this.ContentType:" + this.ContentType);
break;
} fout.write(temp,0,i);
// fout.write(newline.getBytes());
}
}
}
catch (IOException ioe) {
System.out.print(ioe.getMessage());
}
finally {
try {
fout.close();
}
catch (Exception e) {
}
}
System.out.println("***** FINISHED ******"); }
//request.getContentType()方法得到String s
protected void setContentType(String s) {
ContentType = s;
int j;
System.out.println("ContentType.indexOf(\"boundary=\"):" + ContentType.indexOf("boundary="));
if((j = ContentType.indexOf("boundary=")) != -1) {
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
System.out.println("ContentType: " + ContentType);
//it is commonly null
}
//request.getCharacterEncoding()方法得到String s
protected void setCharacterEncoding(String s) {
CharacterEncoding = s;
System.out.println("CharacterEncoding: " + s);
}
//get filename
private String getFileName(ServletInputStream sin) {
String filename = "";
String str = ""; int i = 0;
int j = 0;
try { while ((i = sin.readLine(temp, 0, temp.length)) != -1) {
if (this.CharacterEncoding != null) {
str = new String(temp, 0, i, this.CharacterEncoding);
}
else {
str = new String(temp, 0, i);
}
if ((j = str.indexOf("filename=")) != -1) {
j += 10;
str = str.substring(j);
System.out.println("str:" + str);
// the last string of "
if ((j = str.indexOf("\"")) > 0) {
str = str.substring(0, j);
}
//now str is the file directory in the client pc
if ((j = str.lastIndexOf("\\")) != -1) {
filename = str.substring(j);
}
break;
}
}
}
catch (IOException ioe) {
System.out.println("ERROR");
System.out.println(ioe.getMessage());
} System.out.println("filename:" + filename + ".");
return filename;
}
}
<html>
<head>上传文件</head>
<body>
<hr>
<form method="post" action="/UploadFileServlet" enctype="multipart/form-data">
<p>文件:<input type = "file" name="FileData" size="20"></p>
<p><input type="submit" value="UpLoad" name="uploadfile"></p>
</form>
</body>
</html>