高级项目进行时11

  1、 今天偶然发现Oracle中不使用to_date函数也可以直接插入日期类型的值('2000-2月-05')这种形式的。

         ^_^  ^_^  刚刚遇到时竟觉得不可思议, 后来查看了一些资料才知道这是支持中文的oracle的默认日期格式, 所以其可以不用转换而直接进行插入。

 2、 今天尝试了一下如下的语句: select  *  from XXX where XXX.a in (a,b,null), 结果运行成功 。 这样子启发了自己... ...

3、 struts2中使用KindEditor,  ^_^  ^_^  又是KindEditor...问题: 在kindEditor中使用自定义的upload.jsp上传图片时总是不成功。

        如下是我的upload.jsp源文件:

 ————————————————————————————————————————————--————————————

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ page import="java.util.*,java.io.File"%>
<%@ page import="org.apache.commons.fileupload.*,org.apache.commons.lang.StringUtils,org.apache.commons.lang.time.DateFormatUtils,org.apache.commons.io.FileUtils"%>
<%
 String contextPath = request.getContextPath() ;

 String uploadPath = session.getServletContext().getInitParameter("uploadPath");System.out.println("uploadpath:"+uploadPath);
 String tmpPath = session.getServletContext().getInitParameter("tmpPath");System.out.println("tmppath:"+tmpPath);
 String uploadImageType = session.getServletContext().getInitParameter("uploadImageType");System.out.println("imgType:"+uploadImageType);
 int maxSize = Integer.parseInt ( session.getServletContext().getInitParameter("uploadImageSize") );
 
 String uploadRealPath = session.getServletContext().getRealPath( uploadPath );
 String tmpRealPath = session.getServletContext().getRealPath( tmpPath );
 FileUtils.forceMkdir ( new File ( uploadRealPath ) );
 FileUtils.forceMkdir ( new File ( tmpRealPath ) );
 
 String saveUrl = "/xyy"+uploadPath; System.out.println("saveUrl:"+saveUrl);
 String[] fileType = StringUtils.split ( uploadImageType,"|" ) ;
 
 String msg1 = "上传文件大小超过限制。";
 String msg2 = "上传文件的扩展名不被允许。";
 String msg3 = "文件上传失败。";
 String msg = msg3;
 //java.io.File files=new java.io.File(".");    
 //String FileName = (String)request.getAttribute("fileName");
 String imageWidth = "";
 String imageHeight = "";
 String imageBorder = "";
 String imageTitle = "";
 String imageTitle2 = "";
 String imageAlign = "";
 String imageHspace = "";
 String imageVspace = "";
 String id = "";

 String fileNameAuto = DateFormatUtils.format ( new Date(),"yyyyMMddHHmmss" );
 String filePath = null;
 String fileUrl = null;
 
 DiskFileUpload fu = new DiskFileUpload();
 fu.setSizeMax(maxSize);//
 fu.setSizeThreshold(4096);
 fu.setRepositoryPath(tmpRealPath);
 
 List fileItems = fu.parseRequest(request);
 System.out.println("有得到request没: "+request);
 System.out.println("********** fileItems: "+fileItems+":size="+fileItems.size());
 Iterator iter = fileItems.iterator();
 while (iter.hasNext()) {

  FileItem item = (FileItem) iter.next();
  String fieldName = item.getFieldName();
  if (!item.isFormField()) {
   String name = item.getName();
   long size = item.getSize();
   if ((name == null || name.equals("")) && size == 0)
    continue;
   if (size > maxSize) {
    msg = msg1;
    break;
   }
   int pos = name.lastIndexOf(".");
   String ext = name.substring(pos+1,name.length());
   boolean isLegalFileType = false;
   for (int m = 0; m < fileType.length; m++) {
    if (fileType[m].equalsIgnoreCase(ext)) {
     isLegalFileType = true;
     break;
    }
   }
   if (! isLegalFileType ) {
    msg = msg2;
    break;
   }
   filePath = uploadRealPath + "/" + fileNameAuto + "." + ext;
   fileUrl = saveUrl + "/" + fileNameAuto + "." + ext;
   System.out.println("fileUrl:"+fileUrl);
   java.io.File f = new java.io.File(filePath);
   item.write(f);
  } else {
   String fieldValue = item.getString();
   if ("imgWidth".equals(fieldName)) {
    imageWidth = fieldValue; System.out.println("width: "+imageWidth);
   } else if ("imgHeight".equals(fieldName)) {
    imageHeight = fieldValue; System.out.println("height: "+imageHeight);
   } else if ("imgBorder".equals(fieldName)) {
    imageBorder = fieldValue; System.out.println("border: "+imageBorder);
   } else if ("imgTitle".equals(fieldName)) {
    imageTitle = fieldValue;
   } else if ("imgTitle2".equals(fieldName)) {
    imageTitle = fieldValue;
   } else if ("imgAlign".equals(fieldName)) {
    imageAlign = fieldValue;
   } else if ("imgHspace".equals(fieldName)) {
    imageHspace = fieldValue;
   } else if ("imgVspace".equals(fieldName)) {
    imageVspace = fieldValue;
   }
   else if ("id".equals(fieldName)) {
    id = fieldValue;
   }
  }
 }
 if (fileUrl != null) {
  out.println("<html>");
  out.println("<head>");
  out.println("<title>error</title>");
  out.println("<meta http-equiv=/"content-type/" content=/"text/html; charset=UTF-8/">");
  out.println("</head>");
  out.println("<body>");
  out.println("<script type=/"text/javascript/">parent.KE.plugin[/"image/"].insert(/""
    + id + "/",/""
      + fileUrl
      + "/",/""
      + imageWidth
      + "/",/""
      + imageHeight
      + "/",/""
      + imageBorder
      + "/",/""
      +imageTitle
      + "/",/""
      +imageTitle
      + "/");</script>");
  out.println("</body>");
  out.println("</html>");
 } else {
  out.println("<html>");
  out.println("<head>");
  out.println("<title>error</title>");
  out.println("<meta http-equiv=/"content-type/" content=/"text/html; charset=UTF-8/">");
  out.println("</head>");
  out.println("<body>");
  out.println("<script type=/"text/javascript/">alert(/""
      + msg
      + "/");parent.KindDisableMenu();parent.KindReloadIframe();</script>");
  out.println("</body>");
  out.println("</html>");
 }
%>
  后经过我逐层打印, 最终发现是此页面的List fileItems = fu.parseRequest(request);中fileItems打印为空, 但是让我纳闷的是我以前在servlet中使用此页面时可以正确实现上传功能。 为什么?

   最终通过对比前后的打印语句, 发现两处打印出来的request不同, 在struts2中的upload.jsp中打印出来的request中有struts2 dispatcher...等字样。

  哦, 一切到此终于揪出了这个折磨了我大半天的”魔鬼“。

——考虑请求被拦截,遂修改web.xml,由

<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>

修改为:

<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>*.action</url-pattern>
  </filter-mapping>

问题解决!

————————在网上发现的一个类似的upload.jsp————————————————

<%@page contentType="text/html; charset=UTF-8"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%
try{
//文件保存目录路径
ServletContext sc=this.getServletContext();
String SavePath = sc.getRealPath("/")+"//rules//uploaded//";
System.out.println("SavePath:"+SavePath);
File myFilePath = new File(SavePath);
if (!myFilePath.exists())
        myFilePath.mkdir();

//文件保存目录URL
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
String SaveUrl=basePath+ "rules/uploaded/";
System.out.println("SaveUrl:"+SaveUrl);
//定义允许上传的文件扩展名
ArrayList ExtArr=new ArrayList();
ExtArr.add("gif");
ExtArr.add("jpg");
ExtArr.add("png");
ExtArr.add("bmp");

DiskFileUpload upload = new DiskFileUpload();

System.out.println("rquest:"+request.getRequestURL());
upload.setHeaderEncoding("UTF-8");
upload.setSizeMax(2 * 1024 * 1024);
upload.setSizeThreshold(1 * 1024 * 1024);
upload.setRepositoryPath(System.getProperty("java.io.tmpdir"));


String FileWidth ="";
String FileHeight = "";
String FileBorder = "";
String FileTitle = "";
String FileAlign = "";
String FileHspace = "";
String FileVspace = "";


List fileItems = upload.parseRequest(request);//fileItems总是为空
for (Iterator iter = fileItems.iterator(); iter.hasNext(); )
{
FileItem item = (FileItem) iter.next();
if (item.isFormField()&&item.getName()!=null) {
  if(item.getName().equals("imgWidth"))
     FileWidth=item.getString();
  else if(item.getName().equals("imgHeight"))
     FileHeight=item.getString();
  else if(item.getName().equals("imgBorder"))
     FileBorder=item.getString();
  else if(item.getName().equals("imgTitle"))
     FileTitle=item.getString();
  else if(item.getName().equals("imgAlign"))
     FileAlign=item.getString();
  else if(item.getName().equals("imgHspace"))
     FileHspace=item.getString();
  else if(item.getName().equals("imgVspace"))
     FileVspace=item.getString();
}
if (!item.isFormField()) {
  String fileName = item.getName();
  int pos=fileName.lastIndexOf(".");
  if(pos<=0 || pos==fileName.length()-1)
   break;
  String fileType=fileName.substring(pos+1,fileName.length());
  if(fileType==null || fileType.equals(""))
   break;
  if(ExtArr.indexOf(fileType.toLowerCase())<0)
  {
    out.println( "<script type=/"text/javascript/">alert(/"上传文件扩展名是不允许的扩展名。/");parent.KindDisableMenu();parent.KindReloadIframe();</script>");
  }
  long fileSize = item.getSize();

  InputStream inStream = item.getInputStream();
  String newFileName=System.currentTimeMillis()+"."+fileType;
  FileOutputStream fos = new FileOutputStream(SavePath +newFileName );

  int bytesRead;
    byte[] buf = new byte[4 * 1024]; // 4K buffer
    while ((bytesRead = inStream.read(buf)) != -1)
    {
        fos.write(buf, 0, bytesRead);
    }
    fos.flush();
    fos.close();
    inStream.close();

    String FileUrl = SaveUrl + newFileName;

 out.println( "<html>");
 out.println( "<head>");
 out.println( "<title></title>");
 out.println( "<meta http-equiv=/"content-type/" content=/"text/html; charset=UTF-8/">");
 out.println( "</head>");
 out.println( "<body oncontextmenu=false>");
 out.println( "<script type=/"text/javascript/">parent.KindInsertImage(/"" + FileUrl + "/",/"" + FileWidth + "/",/"" + FileHeight + "/",/"" + FileBorder + "/",/"" + FileTitle + "/",/"" + FileAlign + "/",/"" + FileHspace + "/",/"" + FileVspace + "/");</script>");
 out.println( "</body>");
 out.println( "</html>");
 }
}
}catch(Exception e)
{
 out.println( "<script type=/"text/javascript/">alert(/"" +e.getMessage()  + "/");parent.KindDisableMenu();parent.KindReloadIframe();</script>");
}
%>

 ————————————————————扩展——————————————————

  自我尝试设想: 是否将form表单提交改成window.location.href=""的方式提交, 这样子其是否就可以避免struts2中的过滤器过滤了呢?

4、 有时需要使用一个action过渡到一个jsp页面,该如何做呢?

    很简单, 其与普通的action唯一的不同的是配置, 在配置相应的action时不加class这个属性便是。 如:

   <action name="AjaxTest" class="lee.AjaxTestAction">
<result>/AjaxResult.jsp</result>
</action>
<action name="Test3">
<result>/testjs.jsp</result>
</action>

 AjaxTest就是转向了一个Action,而Test3直接转向一个Jsp 页面。

    其实这时Test3相当于直接使用ActionSupport类作为处理类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值