kindEditor,跪求帮助
后台能获得textarea的内容,但是得不到imgFile(文件名),在String fileName = wrapper.getFileNames("imgFile")[0];//imgFile,imgFile,imgFile
这里提示空指针错误。 能获得textarea内容,配置应该没错。刚弄这个东西,查了好几天不知道怎么办。求哥哥姐姐帮我!
package wyk.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.json.易做图.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
public class AddNewsAction extends ActionSupport{
private File imgFile;
private String imgFileFileName;
private String imgWidth;
private String imgHeight;
private String align;
@Override
public String execute() throws Exception {
HttpServletRequest req=(HttpServletRequest)ServletActionContext.getRequest
();
HttpServletResponse res=(HttpServletResponse)
ServletActionContext.getResponse();
// TODO Auto-generated method stub
PrintWriter out = res.getWriter();
String title=req.getParameter("title").toString();
String content=req.getParameter("content").toString();
System.out.println("标题是"+title);
System.out.println("内容是"+content);
//文件保存目录路径
//D:\Tomcat6.0\webapps\zswz\attached/
String savePath = req.getSession().getServletContext().getRealPath("/") +
"attached/image";
//文件保存目录URL /zswz/attached/
String saveUrl = req.getContextPath() + "/attached/image";
System.out.println("struts2文件保存目录路径"+savePath);
System.out.println("文件保存目录URL"+saveUrl);
//定义允许上传的文件扩展名
//定义允许上传的文件扩展名
HashMap<String, String> extMap = new HashMap<String, String>();
extMap.put("image", "gif,jpg,jpeg,png,bmp");
extMap.put("flash", "swf,flv");
extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
//允许最大上传文件大小 struts.xml struts.multipart.maxSize=3G
long maxSize = 3000000000l;
res.setContentType("text/html; charset=UTF-8");
if(!ServletFileUpload.isMultipartContent(req)){
System.out.println("请选择文件");
out.println(getError("请选择文件"));
return "error";
}
File uploadDir = new File(savePath);
if(!uploadDir.isDirectory()){
System.out.println("上传目录不存在");
out.println(getError("上传目录不存在"));
return "error";
}
String dirName = req.getParameter("dir");//image
if (dirName == null) {
dirName = "image";
}
if(!extMap.containsKey(dirName)){
System.out.println("目录名不正确");
out.println(getError("目录名不正确"));
return "error";
}
savePath += dirName + "/";//D:\Tomcat6.0\webapps\zswz\attached/image/
saveUrl += dirName + "/";///zswz/attached/image/
File saveDirFile = new File(savePath);
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String ymd = sdf.format(new Date());
savePath += ymd + "/";//D:\Tomcat6.0\webapps\zswz\attached/image/20111129/
saveUrl += ymd + "/";///zswz/attached/image/20111129/
File dirFile = new File(savePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
if (!dirFile.isDirectory()) {
out.println(getError("上传目录不存在"));
return "error";
}
//Struts2 请求 包装过滤器
MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) req;
//获得上传的文件名
String fileName = wrapper.getFileNames("imgFile")[0];//imgFile,imgFile,imgFile
System.out.println("文件名是"+fileName);
//获得文件过滤器
File file = wrapper.getFiles("imgFile")[0];
//检查扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
out.println(getError("上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get
(dirName) + "格式。"));
return "error";
}
//检查文件大小
if (file.length() > maxSize) {
out.println(getError("上传文件大小超过限制。"));
return "error";
}
//重构上传图片的名称
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newImgName = df.format(new Date()) + "_"
+ new Random().nextInt(1000) + "." + fileExt;
byte[] buffer = new byte[1024];
//获取文件输出流
FileOutputStream fos = new FileOutputStream(savePath +"/" + newImgName);
//获取内存中当前文件输入流
InputStream in = new FileInputStream(file);
try {
int num = 0;
while ((num = in.read(buffer)) > 0) {
fos.write(buffer, 0, num);
}
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
in.close();
fos.close();
}
//发送给 KE
JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", saveUrl +"/" + newImgName);
out.println(obj.toJSONString());
return "success";
}
private String getError(String message) {
JSONObject obj = new JSONObject();
obj.put("error", 1);
obj.put("message", message);
return obj.toJSONString();
}
}
<script>--------------------编程问答-------------------- MultiPartRequestWrapper wrapper 这个对象没有实例化? --------------------编程问答-------------------- 这个是我从别人那里复制过来的,都是这么写的 --------------------编程问答-------------------- 你这里加个断点跟进去,看看实例化了没 --------------------编程问答-------------------- 实例化了,有值
ke.show({
id:'kind',
uploadJson : 'newsUpload.action',//提交地址,action要自己配置
fileManagerJson : 'houtaimanage/editor/jsp/file_manager_json.jsp',
allowFileManager : true,
afterCreate : function() {
var self = this;
K.ctrl(document, 13, function() {
self.sync();
document.forms['kingEditor'].submit();
});
K.ctrl(self.edit.doc, 13, function() {
self.sync();
document.forms['kingEditor'].submit();
});
}
});
function submit(){
document.getElementById("kind").sync();//同步数据到服务器(图片,视频,音频什么的)//如果没有这一步对不起,你获取不到数据
document.getElementById("kindEditor").submit();
};
</script>
<form id="kindEditor" method="post" action="newsUpload.action" enctype="multipart/form-data">
<textarea id="kind" name="content" style="width:800px;height:400px;visibility:hidden;"></textarea>
<input type="button" id="uploadButton" value="Upload" onclick="submit()"/>
</form>
补充:Java , Web 开发