社区
Web 开发
帖子详情
上传文件,在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
打赏
收藏
上传文件,在form标签里加了enctype="multipart/form-data"属性后,action收到的其它<input>内容中文乱码
jsp代码 <base href="" <form name="form" action="applyNewCourseAction" method="POST" enctype="multipa
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
springmvc ajaxFileUpload
上传文件
(前后台彻底分离的情况下)
首先是导入jar包: web.xml: 1 <servlet> 2 <servlet-name>mvc-dispatcher</servlet-name> 3 <ser...
struts2的
上传文件
与下载文件
前言:在数据库中,我们一般都是保存文件的信息(保存的路径,文件名等等),而不会把文件当作Clob打字段保存在数据库中。所以
上传文件
和下载文件的思路就清晰了很多。限于时间,这里只记录大概思路,具体详细
内容
...
SpringMVC
课程主要
内容
: 1. springMVC入门 1.1 简介 1.2 入门案例 helloworld pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=...
asp经典例子
<
input
type=submit value=submit></
form
> <SCRIPT LANGUAGE="JavaScript"><!-- function mm() { var s = document.
form
1.meizz.value; if(s=="")return false; var img = new Image(); img.src = s; ...
PHP笔记(二)核心技术
9.文件处理 9.1打开文件resource fopen(filename, mode),其中mode是打开文件的方式,mode包括r只读、r+只读、w、w+、 x、x+;关闭文件bool fclose(resource handle) 9.2读写文件 int readfile...
Web 开发
81,092
社区成员
341,717
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章