JSP文件上传传过去的名字总是乱码,代码如下: 5
<%@pagecontentType="text/html;charset=GBK"pageEncoding="UTF-8"%><%@pageimport="java.i...
<%@ page contentType="text/html; charset=GBK" pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传文件</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
int max = 102400*102400;
String rootpath; /*保存路径*/
DataInputStream in=null;
FileOutputStream fileout=null;
rootpath = "E:/Users/scr/";
String contentType = request.getContentType();
try{
if(contentType.indexOf("multipart/form-data")>=0)
{
in = new DataInputStream(request.getInputStream()); //读入上传数据
int formDataLength = request.getContentLength();
if(formDataLength>max)
{
out.print("<p>上传文件太大。</p>");
return;
}
byte dataByte[] = new byte[formDataLength]; //保存上传数据
int byteread = 0;
int totalbyte = 0;
while(totalbyte<formDataLength)
{
byteread = in.read(dataByte,totalbyte,formDataLength);
totalbyte += byteread;
}
String file = new String(dataByte);
String saveFile = file.substring(file.indexOf("filename=\"")+10);
saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex+1,contentType.length());
String fileNameone = rootpath+saveFile;
byte[] b = fileNameone.getBytes("ISO-8859-1");
String fileName = new String(b,"utf-8");
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n",pos)+1;
pos = file.indexOf("\n",pos)+1;
pos = file.indexOf("\n",pos)+1;
int boundaryLocation = file.indexOf(boundary,pos)-4;
int startPos = ((file.substring(0,pos)).getBytes()).length;
int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
File checkFile = new File(fileName);
if(checkFile.exists())
{
out.print("<p>"+saveFile+"文件已存在。</p>");
}
fileout = new FileOutputStream(fileName);
fileout.write(dataByte,startPos,(endPos-startPos));
fileout.close();
out.print("<p>文件上传成功。</p>");
}
}catch(Exception ex)
{
throw new ServletException(ex.getMessage());
}
%>
</body>
</html> 展开
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传文件</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
int max = 102400*102400;
String rootpath; /*保存路径*/
DataInputStream in=null;
FileOutputStream fileout=null;
rootpath = "E:/Users/scr/";
String contentType = request.getContentType();
try{
if(contentType.indexOf("multipart/form-data")>=0)
{
in = new DataInputStream(request.getInputStream()); //读入上传数据
int formDataLength = request.getContentLength();
if(formDataLength>max)
{
out.print("<p>上传文件太大。</p>");
return;
}
byte dataByte[] = new byte[formDataLength]; //保存上传数据
int byteread = 0;
int totalbyte = 0;
while(totalbyte<formDataLength)
{
byteread = in.read(dataByte,totalbyte,formDataLength);
totalbyte += byteread;
}
String file = new String(dataByte);
String saveFile = file.substring(file.indexOf("filename=\"")+10);
saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex+1,contentType.length());
String fileNameone = rootpath+saveFile;
byte[] b = fileNameone.getBytes("ISO-8859-1");
String fileName = new String(b,"utf-8");
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n",pos)+1;
pos = file.indexOf("\n",pos)+1;
pos = file.indexOf("\n",pos)+1;
int boundaryLocation = file.indexOf(boundary,pos)-4;
int startPos = ((file.substring(0,pos)).getBytes()).length;
int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
File checkFile = new File(fileName);
if(checkFile.exists())
{
out.print("<p>"+saveFile+"文件已存在。</p>");
}
fileout = new FileOutputStream(fileName);
fileout.write(dataByte,startPos,(endPos-startPos));
fileout.close();
out.print("<p>文件上传成功。</p>");
}
}catch(Exception ex)
{
throw new ServletException(ex.getMessage());
}
%>
</body>
</html> 展开
4个回答
展开全部
charset=GBK" pageEncoding="UTF-8"%>
pageEncoding是jsp文件本身的编码
contentType的charset是指服务器发送给客户端时的内容编码
JSP要经过两次的“编码”,第一阶段会用pageEncoding,第二阶段会用utf-8至utf-8,第三阶段就是由Tomcat出来的网页, 用的是contentType。
第一阶段是jsp编译成.java,它会根据pageEncoding的设定读取jsp,结果是由指定的编码方案翻译成统一的UTF-8 JAVA源码(即.java),如果pageEncoding设定错了,或没有设定,出来的就是中文乱码。
第二阶段是由JAVAC的JAVA源码至java byteCode的编译,不论JSP编写时候用的是什么编码方案,经过这个阶段的结果全部是UTF-8的encoding的java源码。
pageEncoding是jsp文件本身的编码
contentType的charset是指服务器发送给客户端时的内容编码
JSP要经过两次的“编码”,第一阶段会用pageEncoding,第二阶段会用utf-8至utf-8,第三阶段就是由Tomcat出来的网页, 用的是contentType。
第一阶段是jsp编译成.java,它会根据pageEncoding的设定读取jsp,结果是由指定的编码方案翻译成统一的UTF-8 JAVA源码(即.java),如果pageEncoding设定错了,或没有设定,出来的就是中文乱码。
第二阶段是由JAVAC的JAVA源码至java byteCode的编译,不论JSP编写时候用的是什么编码方案,经过这个阶段的结果全部是UTF-8的encoding的java源码。
展开全部
用记事本打开.jsp,另存为ANSI。修改pageEncoding为GBK,head标签内部加“<meta http-equiv="Content-Type" content="text/html; charset=gbk" />”;body里<%..%>第一行前加request.setCharacterEncoding="GBK"。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
pageEncoding="GBK",页面编码也要设置为GBK格式的!
追问
还是没用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将
UTF-8
改成gbk
UTF-8
改成gbk
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询