当前位置: 技术问答>java相关
在form.action="servlet?china=中文" 附带了中文参数,但servlet里面得到的china=null,请问只用这种形式传递参数china,究竟有没有办法
来源: 互联网 发布时间:2015-09-28
本文导语: 在form.action="/tech-qa-java/servlet/china/中文.html" 附带了中文参数,但servlet里面得到的china=null,请问只用这种形式传递参数china,究竟有没有办法解决这个问题的,这好像和其他的解决中文的例子不同哦? | 你...
在form.action="/tech-qa-java/servlet/china/中文.html" 附带了中文参数,但servlet里面得到的china=null,请问只用这种形式传递参数china,究竟有没有办法解决这个问题的,这好像和其他的解决中文的例子不同哦?
|
你不要用form.action的方法,应该用hidden字段
...
这样如果需要的话,还可以在js中修改china的值
...
这样如果需要的话,还可以在js中修改china的值
|
完全赞同楼上,后者,对中文进行encode一下,用javascript
|
这样传中文是不行的,encode等我都试过,无论怎样转换都不行,我是用变能的方法。
|
form的method用doGet方法
|
doGet是url传值,
doPost是body传值。
要上传文件只能用doPost。
/**
*
*
*
*
* @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;
}
}
上传文件
doPost是body传值。
要上传文件只能用doPost。
/**
*
Title: UploadFileServlet
*
Description: Upload a file to the serve
*
Copyright: Copyright (c) 2002
*
Company: shadow
* @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;
}
}
上传文件
文件: