今天我来总结昨天开发的一个简单的jsp web 应用程序时,在做一个调教表单,从servlet端获取数据,这个表单里含有上传文件控件。如果我们在测试的时候,获取数据的是乱码,这时,大家可以先去掉上传控件的,然后测试通过get请求向servlet传数据,通过

response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();

  进行输出中文测试,如果出现乱码,自己去处理页面的编码,以及设置上面的编码方式;这个问题如果解决了,那么在把上传控件放到表单里,再测试!

大家知道,在jsp里,表单里含有上传文件控件的,在服务端,我们大部分采用这样的处理方式:先导入commons-fileupload-1.2.1.jar和commons-io-1.4.jar这两个Jar文件;然后在servlet里这样写:

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		DiskFileUpload diskFileUpload = new DiskFileUpload();
		diskFileUpload.setHeaderEncoding("utf-8");
		diskFileUpload.setSizeMax(100 * 1024 * 1024);
		diskFileUpload.setSizeThreshold(4096);
		String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "uploads\\";
		diskFileUpload.setRepositoryPath(savePath);

		try {
			List fileItems = diskFileUpload.parseRequest(request);
			Iterator iter = fileItems.iterator();
			Ads model = new Ads();
			PrintWriter writer = response.getWriter();
			for (; iter.hasNext();)
			{
				FileItem fileItem = (FileItem) iter.next();
				if (fileItem.isFormField()) { /* 一般的字段判断 */

					if(fileItem.getFieldName().equalsIgnoreCase("catename"))
					{
						model.setCatename(fileItem.getString("utf-8"));

					}
				}
				else {
                        /* 控件上传*/
					if(fileItem.getFieldName().equalsIgnoreCase("cateicon"))
					{
						String fileName = fileItem.getName();
						if(!StringUtil.isNullOrEmpty(fileName))
						{
							fileItem.write(new File(savePath + fileName));
							model.setCicon(fileName);
						}

					}                        /* 如果表单中有多个控件,继续处理 */
					else if(fileItem.getFieldName().equalsIgnoreCase("lefttop"))
					{
						String fileName = fileItem.getName();
						if(!StringUtil.isNullOrEmpty(fileName))
						{
							fileItem.write(new File(savePath + fileName));
							model.setLefttop(fileName);
						}
					}

				}
			}
			model.setOrderid(1);
			AdsDao.add(model);
			writer.println("<script>alert('添加成功');location.href='/AdsList.jsp';</script>");
			writer.flush();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

   在上面,我标注一段用红色表示的

diskFileUpload.setHeaderEncoding("utf-8");

它一定要设置自己需要的编码方式,这样就不会出现问题了!

转载请注明http://www.cnblogs.com/yushengbo,否则将追究版权责任!

淘宝(阿里百川)手机客户端开发日记第十四篇 jsp提交含有上传控件表单乱码问题的更多相关文章

  1. 淘宝(阿里百川)手机客户端开发日记第十五篇 JSON解析(四)

    解析一个从淘宝传递的JSON (大家如有兴趣可以测试下):{ "tae_item_detail_get_response": { "data": { " ...

  2. 淘宝(阿里百川)手机客户端开发日记第十二篇 mysql的认识

    我这里用的是wamp,大家可以到网上去下载对应的包,自行安装,对于程序员来讲,安装软件大部分都应该不是问题的,所以我不去将具体安装的方法. wamp安装好后,在我们屏幕的右下角, 这样的图标,我们右键 ...

  3. 淘宝(阿里百川)手机客户端开发日记第十篇 阿里百川服务器环境介绍之API文档的快速链接(四)

    个人感觉比较重要的快速链接: http://open.taobao.com/doc/detail.htm?id=102513 http://open.taobao.com/doc/detail.htm ...

  4. 淘宝(阿里百川)手机客户端开发日记第十一篇 JSP+Servlet

    由于本人从事.net开发已有多年经验,今天由于工作需要,我只能学习下JSP+Servlet,至于java web提供了更好的开发框架MVC,现在由于时间关系,我只好用JSP+Servlet来搭建服务器 ...

  5. 淘宝(阿里百川)手机客户端开发日记第一篇 android 主框架搭建(一)

    android 主框架搭建(一) 1.开发环境:Android Studio 相继点击下一步,直接项目建立完毕(如下图) 图片看的效果如果很小,请放大您的浏览器显示百分比  转载请注明http://w ...

  6. 淘宝(阿里百川)手机客户端开发日记第九篇 Looper详解

    public final class Looper: 官方的API: Class used to run a message loop for a thread. Threads by default ...

  7. 淘宝(阿里百川)手机客户端开发日记第五篇 SharedPreferences使用详解

    我们知道,Android中数据存储技术由于如下几种 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用ContentProvider存储数据 ...

  8. 淘宝(阿里百川)手机客户端开发日记第十三篇 mysql的连接

    首先,我建立了一个包,里面存放了三个类文件,这三个文件是我从网络中找的,经过自己的整理.(我刚才查找想把这三个文件传上去,可能是自己对cnblogs的博客不太熟悉吧,没有找到,我只好粘贴代码了) 三个 ...

  9. 淘宝(阿里百川)手机客户端开发日记第八篇 Handler的使用方法

    首先,我们先看下API文档的说明: A Handler allows you to send and process Message and Runnable objects associated w ...

随机推荐

  1. 网站缓存数据到tomcat服务器

    通过缓存使相同的数据不用重复加载,降低数据库的访问 public class CacheFilter implements Filter { //实例变量[每线程共享] private Map< ...

  2. SQL Server 2012 学习笔记3 增查改删

    现在举例几个"增查改删"的语句 select * from UserInfor --查找所有字段 select username,UserId from UserInfor -- ...

  3. char *p = "abcdefg"; p[0] = p[1]出错

    参考:http://blog.sina.com.cn/s/blog_5c0172280100ut4o.html 1.char *s="abc"; 看这个赋值: 右边,是" ...

  4. WinForm------TreeListLookUpEdit控件的使用

    1.数据库添加表dbo.Graduation 2.从工具栏拖出TreeListLookUpEdit控件,修改部分属性 Display Name:选中后显示在控件的值 Value Member:C#代码 ...

  5. WinForm------给GridControl添加搜索功能

    //按钮点击事件 private void Btn_Search_Click(object sender, EventArgs e) { //获取编辑框的值 string text = this.te ...

  6. nltk.download()出错解决

    http://blog.csdn.net/joey_su/article/details/17289621 官方下载地址 http://www.nltk.org/nltk_data/ 把python自 ...

  7. ubuntu下安装TexLive和Texmaker

    也可以参考ubuntu14.04配置中文latex完美环境(texlive+texmaker+lyx) 设置中文字体的时候参考ubuntu 下安装 texlive 并设置 ctex 中文套装 1.首先 ...

  8. Unity Editor开发

    SerializedObject SerializedObject.Update()更新所有序列化对象的值:SerializedObject.ApplyModifiedProperties()应用序列 ...

  9. Occlusion Culling

    遮挡剔除 http://www.bjbkws.com/online/1092/ unity遮挡剔除(应用) http://www.unitymanual.com/thread-37302-1-1.ht ...

  10. Quagga服务器安装和配置

    使用本地源 一.安装软件包 # yum install quagga-0.99.15-7.el6_3.2.x86_64.rpm 或rpm   # ls /etc/quagga/ bgpd.conf.s ...