
java web 中传到linux服务器后从就表单获得数据后乱码
我的思路是通过一个表单来从浏览器获得数据,主要代码如下(因为我要通过表单来上传一个图片,我用了jsmartcom插件):<%@pagelanguage="java"con...
我的思路是
通过一个表单来从浏览器获得数据,主要代码如下(因为我要通过表单来上传一个图片,我用了jsmartcom插件):
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>上传文件</title>
</head>
<body>
<form id="form1" name="form1" enctype="multipart/form-data" method = "post" action="../upload.jsp" >
<center>
<table width="707" height="519" border="0">
<tr>
<td height="38" colspan="3">输入产品信息:</td>
</tr>
<tr>
<td width="111">商品名称:</td>
<td width="301" height="50"><label></label> <div align="left">
<input type="text" name="name" onChange="chang1(this)">
</div></td>
<tr>
<td width="111">上传图片:</td>
<td height="64"><div align="left">
<input type = "file" name = "File1" size = "20" maxlength = "30" onChange="chang4(this)"/>
</div></td>
<td id="file"><label><br>
</label></td>
</tr>
<input type="button" name="Submit2" value="重置" onClick="res()"/>
<input type="button" name="Submit3" value="提交" onClick="sub()"/> </td>
</tr>
</table>
</center>
</form>
</body>
</html>
获得数据后,交由下面代码处理
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.io.PrintWriter"%>
<jsp:useBean id="smartupload" class="com.jspsmart.upload.SmartUpload"/>
<html>
<head>
<title>upload page</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8") ;
response.setContentType("text/html;charset=utf-8");
smartupload.initialize(pageContext) ; // 初始化上传
smartupload.setAllowedFilesList("png,gif,jpg");
smartupload.setMaxFileSize(1000000);
smartupload.upload() ; // 准备上传
String pic = getCode();
pic = pic + "." + smartupload.getFiles().getFile(0).getFileExt() ;
String fileName = this.getServletContext().getRealPath("/") + "upload/" + pic ;
smartupload.getFiles().getFile(0).saveAs(fileName) ;
%>
<%
pic="upload/"+pic;
String name= smartupload.getRequest().getParameter("name");
request.setAttribute("name", name);
%>
<jsp:forward page="servlet/uploadServlet"/>
</body>
</html>
转到上面页面时在 linux服务器下 我打出了从表单传来的数据显示乱码
怎么解决乱码问题
之后我通过一个servlet把数据插入数据库.
我写的过滤器主函数是
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
chain.doFilter(request, response);
}
是不是因为在 表单上传时用的格式为:enctype="multipart/form-data"
获得数据时为 String name= smartupload.getRequest().getParameter("name");
因而导致过滤器无效???
谢谢
由于字数限制我就把大概代码描述了一下
谢谢 展开
通过一个表单来从浏览器获得数据,主要代码如下(因为我要通过表单来上传一个图片,我用了jsmartcom插件):
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>上传文件</title>
</head>
<body>
<form id="form1" name="form1" enctype="multipart/form-data" method = "post" action="../upload.jsp" >
<center>
<table width="707" height="519" border="0">
<tr>
<td height="38" colspan="3">输入产品信息:</td>
</tr>
<tr>
<td width="111">商品名称:</td>
<td width="301" height="50"><label></label> <div align="left">
<input type="text" name="name" onChange="chang1(this)">
</div></td>
<tr>
<td width="111">上传图片:</td>
<td height="64"><div align="left">
<input type = "file" name = "File1" size = "20" maxlength = "30" onChange="chang4(this)"/>
</div></td>
<td id="file"><label><br>
</label></td>
</tr>
<input type="button" name="Submit2" value="重置" onClick="res()"/>
<input type="button" name="Submit3" value="提交" onClick="sub()"/> </td>
</tr>
</table>
</center>
</form>
</body>
</html>
获得数据后,交由下面代码处理
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.io.PrintWriter"%>
<jsp:useBean id="smartupload" class="com.jspsmart.upload.SmartUpload"/>
<html>
<head>
<title>upload page</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8") ;
response.setContentType("text/html;charset=utf-8");
smartupload.initialize(pageContext) ; // 初始化上传
smartupload.setAllowedFilesList("png,gif,jpg");
smartupload.setMaxFileSize(1000000);
smartupload.upload() ; // 准备上传
String pic = getCode();
pic = pic + "." + smartupload.getFiles().getFile(0).getFileExt() ;
String fileName = this.getServletContext().getRealPath("/") + "upload/" + pic ;
smartupload.getFiles().getFile(0).saveAs(fileName) ;
%>
<%
pic="upload/"+pic;
String name= smartupload.getRequest().getParameter("name");
request.setAttribute("name", name);
%>
<jsp:forward page="servlet/uploadServlet"/>
</body>
</html>
转到上面页面时在 linux服务器下 我打出了从表单传来的数据显示乱码
怎么解决乱码问题
之后我通过一个servlet把数据插入数据库.
我写的过滤器主函数是
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
chain.doFilter(request, response);
}
是不是因为在 表单上传时用的格式为:enctype="multipart/form-data"
获得数据时为 String name= smartupload.getRequest().getParameter("name");
因而导致过滤器无效???
谢谢
由于字数限制我就把大概代码描述了一下
谢谢 展开
展开全部
enctype="multipart/form-data"
使用multipart/form-data上传时,发送的请求和一般的http不一样,需要转化后才能读其他参数。
enctype=\"multipart/form-data\"是上传二进制数据; form里面的input的值以2进制的方式传过去。
form里面的input的值以2进制的方式传过去,所以request就得不到值了。 也就是说加了这段代码,用request就会传递不成功,
使用multipart/form-data上传时,发送的请求和一般的http不一样,需要转化后才能读其他参数。
enctype=\"multipart/form-data\"是上传二进制数据; form里面的input的值以2进制的方式传过去。
form里面的input的值以2进制的方式传过去,所以request就得不到值了。 也就是说加了这段代码,用request就会传递不成功,
追问
对呀,所以我用String name= smartupload.getRequest().getParameter("name");来得到数据
在windows下能正常显示,传到linux服务器下乱码。我也感觉是使用multipart/form-data类型的问题
我怎么才能在使用这种类型的同时也能到表单数据且不乱码。谢谢
追答
name = new String(name.getBytes(),"UTF-8");
name = new String(name.getBytes(),"gb2312");
总要试出来一个
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询