上传文件,在form标签里加了enctype="multipart/form-data"属性后,action收到的其它内容中文乱码

封号码农 2014-05-15 06:25:31
jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<base href="<%=basePath%>"
</head>
<body>
<form name="form" action="applyNewCourseAction" method="POST" enctype="multipart/form-data">
<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC" >
<tr>
<th width="20%" bgcolor="#EEEEEE" scope="row">课程名称</th>
<td bgcolor="#FFFFFF"><div class="FrameDivNormal" id="usrFrameDiv"><input name="courseName" type="text" id="courseName" maxlength="16" ></div>
</td>
</tr>
<tr>
<th width="20%" bgcolor="#EEEEEE" scope="row">课程简介</th>
<td bgcolor="#FFFFFF"><div class="FrameDivNormal" id="usrFrameDiv"><textarea rows="5" cols="90" name="courseShortIntro"></textarea></div>
</td>
</tr>
<tr>
<th width="20%" bgcolor="#EEEEEE" scope="row">课程详细介绍</th>
<td bgcolor="#FFFFFF"><div class="FrameDivNormal" id="usrFrameDiv"><textarea rows="10" cols="90" name="courseIntro"></textarea></div>
</td>
</tr>
<tr>
<th bgcolor="#EEEEEE" scope="row">选择课程封面</th>
<td bgcolor="#FFFFFF">
<input type="file" name="upload"></td>
</tr>
<tr>
<th bgcolor="#EEEEEE" scope="row"> </th>
<td bgcolor="#FFFFFF">
<input type="submit" name="submit" value="提交" >        
<input type="button" onclick="javascript:history.back(-1);" value="返回" >
</td>
</tr>
</table>
</form>
</body>
</html>

接受的action
public class CourseOperateAction extends ActionSupport {
private CourseDAO courseDao;
private String courseName;
private String courseShortIntro;
private String courseIntro;

private File upload;
private String uploadFileName;
private String savePath;
<--省略get和set-->
private static void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), 16 * 1024);
out = new BufferedOutputStream(new FileOutputStream(dst),
16 * 1024);
byte[] buffer = new byte[16 * 1024];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String addCourse() throws Exception {
HttpSession session=ServletActionContext.getRequest().getSession();
courseName=new String(courseName.getBytes("iso-8859-1"),"GBK");
courseShortIntro=new String(courseShortIntro.getBytes("iso-8859-1"),"GBK");
courseIntro=new String(courseIntro.getBytes("iso-8859-1"),"GBK");

Course course=new Course();
course.setCName(courseName);
course.setCFlash(courseShortIntro);
course.setCIntro(courseIntro);
course.setCCheck("0");
course.setTId((Long)session.getAttribute("userid"));
String dstPath="E:\\Graduate design\\mooc\\WebRoot\\images\\course\\"+ " " + (Long)session.getAttribute("userid")+this.getUploadFileName();
System.out.println(dstPath);
File dstFile = new File(dstPath);
copy(this.upload, dstFile);
String c_pic="images/course/"+(Long)session.getAttribute("userid")+this.getUploadFileName();

course.setCPic(c_pic);
courseDao.save(course);
return SUCCESS;
}
}


所有的编码格式都为GBK。
...全文
416 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2014-05-16
  • 打赏
  • 举报
回复
ourseName=new String(courseName.getBytes("iso-8859-1"),"GBK"); courseShortIntro=new String(courseShortIntro.getBytes("iso-8859-1"),"GBK"); courseIntro=new String(courseIntro.getBytes("iso-8859-1"),"GBK"); 这些先不转,获取先看看。
samuel072 2014-05-16
  • 打赏
  • 举报
回复
首先一点 就是使用上传功能必须是post提交,而使用post提交就必须需要再Servlet中 接收参数前设置你的编码方式,使用ServletActionContext 来设置编码方法 你试试看
DwGoing 2014-05-16
  • 打赏
  • 举报
回复
你看下数据库的编码格式
封号码农 2014-05-15
  • 打赏
  • 举报
回复
写过过滤器,无效就撤了 package filter; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class SetCharacterEncodingFilter extends HttpServlet implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; // Handle the passed-in FilterConfig public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) { this.ignore = true; } else if (value.equalsIgnoreCase("true")) { this.ignore = true; } else if (value.equalsIgnoreCase("yes")) { this.ignore = true; } else { this.ignore = false; } } // Process the request/response pair public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) { request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); } } // Pass control on to the next filter chain.doFilter(request, response); } protected String selectEncoding(ServletRequest request) { return (this.encoding); } // Clean up resources public void destroy() { this.encoding = null; this.filterConfig = null; } }
封号码农 2014-05-15
  • 打赏
  • 举报
回复
不好意思,说错了,是在<form> 把method属性设置为POST时乱码,不管enctype的事
teemai 2014-05-15
  • 打赏
  • 举报
回复
直接打印这个看下先:courseName 为什么不写过滤器
封号码农 2014-05-15
  • 打赏
  • 举报
回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧