当前位置:编程学习 > JAVA >>

使用了commons.fileupload.jar包后获取不到textarea的内容

请教一下大神们.下面的代码使用了commons.fileupload.jar包去进行上传文件.也是用了ckeditor 
问题是现在在servlet 获取 textarea的值时候获取不了数据.其他的都可以正常获取 就是textarea这个name为content的获取不到数据..求大神解决..坐等


------------------------------------------------------------------------------------
这是页面代码
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>新增平台</title>
<script type="text/javascript"
src="/gold/admin/index/ckeditor/ckeditor.js"></script>
<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">
-->
<style type="text/css">
* {
font-family: "宋体";
font-size: 14px
}
</style>
</head>

<body>
后台管理系统>>系统管理>>修改平台

<form action="/gold/UpdatePlatServlet" method="post" enctype="multipart/form-data" onsubmit="return check()">
<table>
<tr>
<td>
公司全称
</td>
<td>
<input type="text" name="company" id="company" value="${plat.company }" />
<input type="hidden" name="id" value="${plat.id }" />
</td>
</tr>
<tr>
<td>
公司简称
</td>
<td>
<input type="text" name="name" id="name" value="${plat.name }" />
</td>
</tr>
<tr>
<td>
公司资质
</td>
<td>
<input type="text" name="qualification" id="qualification"
value="${plat.qualification }" />
</td>
</tr>
<tr>
<td>
点击数(数字越大则越前)
</td>
<td>
<input type="text" name="count" id="count" value="${plat.count }" />
</td>
</tr>
<tr>
<td>
公司官网
</td>
<td>
<input type="text" name="url" id="url" value="${plat.url }" />
</td>
</tr>
<tr>
<td>
公司电话
</td>
<td>
<input type="text" name="tel" id="tel" value="${plat.tel }" />
</td>
</tr>
....此处省略
<tr>
<td valign="top">
公司介绍:
</td>
<td>
<textarea cols="80" id="content" name="content" >${plat.content }</textarea>
<script type="text/javascript">
CKEDITOR.replace('content', {
filebrowserUploadUrl : '/gold/ckeditor/uploader?Type=File',
filebrowserImageUploadUrl : '/gold/ckeditor/uploader?Type=Image',
filebrowserFlashUploadUrl : '/gold/ckeditor/uploader?Type=Flash'
});
</script>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>


------------------------------------------------------------------------------

这是servlet
package servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.sun.xml.internal.bind.v2.runtime.Name;

import util.NowDate;
import biz.PlatFormBiz;
import biz.impl.PlatFormBizImpl;
import entity.Platform;

public class UpdatePlatServlet extends HttpServlet {

/**
 * The doPost method of the servlet. <br>
 * 
 * This method is called when a form has its tag value method equals to
 * post.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final long MAX_SIZE = 4 * 1024 * 1024;// 设置上传文件最大为 4M
// 允许上传的文件格式的列表
final String[] allowedExt = new String[] { "jpg", "jpeg", "gif", "png" };
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
String company = "";
String qualification = "";
int id = 0;
int count = 0;
String name = "";
String content = "";
String address = "";
String tel = "";
String url = "";
PlatFormBiz platFormBiz = new PlatFormBizImpl();
Platform platform = null;
// 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(4096);// 设置上传文件时用于临时存放文件的内存大小,这里是4K.多于的部分将临时存在硬盘
dfif.setRepository(new File(request.getRealPath("/") + "img"));// 设置存放临时文件的目录,web根目录下的img目录
// 用以上工厂实例化上传组件
ServletFileUpload sfu = new ServletFileUpload(dfif);
// 设置最大上传尺寸
sfu.setSizeMax(MAX_SIZE);
// 从request得到 所有 上传域的列表
List fileList = null;
try {
fileList = sfu.parseRequest(request);
} catch (FileUploadException e) {// 处理文件尺寸过大异常
if (e instanceof SizeLimitExceededException) {
out.println("<script>alert('文件尺寸超过规定大小:" + MAX_SIZE
+ "字节');</script>");
return;
}
e.printStackTrace();
}
// 得到所有上传的文件
Iterator fileItr = fileList.iterator();
// 循环处理所有文件
while (fileItr.hasNext()) {
FileItem fileItem = null;
String path = null;
long size = 0;
// 得到当前文件
fileItem = (FileItem) fileItr.next();
// 获取表单普通域的数据
if (fileItem.isFormField()) {
if ("content".equals(fileItem.getFieldName())) {
content = new String(fileItem.getString().getBytes(
"iso-8859-1"), "gbk");
}
if ("company".equals(fileItem.getFieldName())) {
company = new String(fileItem.getString().getBytes(
"iso-8859-1"), "gbk");
}
if ("name".equals(fileItem.getFieldName())) {
name = new String(fileItem.getString().getBytes(
"iso-8859-1"), "gbk");
}
if ("id".equals(fileItem.getFieldName())) {
id = Integer.parseInt(fileItem.getString());
}
if ("count".equals(fileItem.getFieldName())) {
count = Integer.parseInt(fileItem.getString());
}
if ("qualification".equals(fileItem.getFieldName())) {
qualification = new String(fileItem.getString().getBytes(
"iso-8859-1"), "gbk");
}
if ("tel".equals(fileItem.getFieldName())) {
tel = new String(fileItem.getString()
.getBytes("iso-8859-1"), "gbk");
}
if ("url".equals(fileItem.getFieldName())) {
url = new String(fileItem.getString()
.getBytes("iso-8859-1"), "gbk");
}
if ("address".equals(fileItem.getFieldName())) {
address = new String(fileItem.getString().getBytes(
"iso-8859-1"), "gbk");
}

platform = platFormBiz.findById(id);
continue;
} else {
// 得到文件的完整路径
path = fileItem.getName();
// 得到文件的大小
size = fileItem.getSize();
if ("".equals(path) || size == 0) {
platform.setAddress(address);
platform.setCompany(company);
platform.setContent(content);
platform.setTel(tel);
platform.setUrl(url);
platform.setName(name);
platform.setContent(content);
platform.setCount(count);
platform.setQualification(qualification);
NowDate nowDate = new NowDate();
String addtime = nowDate.nowDate();
platform.setAddtime(addtime);
int reg = platFormBiz.update(platform);
if (reg == 1) {
out
.print("<script>alert('修改成功');location.href='/gold/findPlatServlet';</script>");
} else {
out
.print("<script>alert('修改失败');location.href='/gold/findPlatServlet';</script>");
}
return;
}
// 得到去除路径的文件名
String t_name = path.substring(path.lastIndexOf("\\") + 1);
// 得到文件的扩展名(无扩展名时将得到全名)
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
// 拒绝接受规定文件格式之外的文件类型
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for (; allowFlag < allowedExtCount; allowFlag++) {
if (allowedExt[allowFlag].equals(t_ext))
break;
}
if (allowFlag == allowedExtCount) {
for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
out.println("<script>alert('请上传以下类型的文件*."
+ allowedExt[allowFlag]
+ "   ');</script>");
return;
}

long now = System.currentTimeMillis();
// 根据系统时间生成上传后保存的文件名
String prefix = String.valueOf(now);
// 保存的最终文件完整路径,保存在web根目录下的img目录下
String u_name = request.getRealPath("/") + "img/" + prefix
+ "." + t_ext;
String pic = "/gold/img/" + prefix + "." + t_ext;
try {
// 保存文件
fileItem.write(new File(u_name));
platform.setAddress(address);
platform.setCompany(company);
platform.setContent(content);
platform.setPic(pic);
platform.setName(name);
platform.setTel(tel);
platform.setUrl(url);
platform.setCount(count);
platform.setContent(content);
platform.setQualification(qualification);
NowDate nowDate = new NowDate();
String addtime = nowDate.nowDate();
platform.setAddtime(addtime);
int reg = platFormBiz.update(platform);
if (reg == 1) {
out
.print("<script>alert('修改成功');location.href='/gold/findPlatServlet';</script>");
} else {
out
.print("<script>alert('修改失败');location.href='/gold/findPlatServlet';</script>");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


}

java web 上传 textarea --------------------编程问答-------------------- <textarea cols="80" id="content" name="content" >${plat.content }</textarea>那么多英文,为什么非得用content当做名字,改为别的名字就差不多了 --------------------编程问答--------------------
引用 1 楼 fangmingshijie 的回复:
<textarea cols="80" id="content" name="content" >${plat.content }</textarea>那么多英文,为什么非得用content当做名字,改为别的名字就差不多了


和这个没关系吧.已经改过也试过了.不行 --------------------编程问答-------------------- post提交:
if ("content".equals(fileItem.getFieldName())) {
content = new String(fileItem.getString().getBytes(
"iso-8859-1"), "gbk");//这里应该是utf8
}

顺便看下你的ckeditor.js引入是否正则,先测试下,是否能正确引入。

还有,如果不处理,默认获取ckeditor的值是包含标签的。
--------------------编程问答--------------------
引用 3 楼 fangmingshijie 的回复:
post提交:
if ("content".equals(fileItem.getFieldName())) {
content = new String(fileItem.getString().getBytes(
"iso-8859-1"), "gbk");//这里应该是utf8
}

顺便看下你的ckeditor.js引入是否正则,先测试下,是否能正确引入。

还有,如果不处理,默认获取ckeditor的值是包含标签的。


和编码问题没关系吧.我调试了一下.表单返回的文本域连textarea的name都获取不了
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,