上传文件,在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。