jimingminlovefly

          統計

          最新評論

          struts2的上傳功能(解決struts2項目中不能用jspsmart上傳)

          struts2的上傳功能(解決struts2項目中不能用jspsmart上傳)

          1.
          <tr>
              <td align="right">廣告圖片<font color="red">*</font>:</td>
              <td align="left">
              <input type="button" id="show_upload_pic_but" name="show_upload_pic_but" value="上傳圖片" onclick="showUploadWin('ad_upload','callbackFun')"/>
              <div id="pic_content">
               
              </div>
              </td>
          </tr>

          2.
          function showUploadWin(imagePath,callbackName){
           try{
              var content=[];
            content.push("<table id='upload_tabel'>");
            content.push("<tr>");
            content.push("<td align='center' style='padding: 10px;'>");
            content.push('<form name="upload_pic_form" id="upload_pic_form" enctype="multipart/form-data" method="post" action="imgUpload.action"  target="upload_frame" onsubmit="return checkUploadPic();">' );  
            content.push('<input type="hidden" name="imagePath" value="'+imagePath+'" />'); 
            content.push('<input type="hidden" name="callbackName" value="'+callbackName+'" />'); 
            content.push('<div style="margin-top:20px;">');
            content.push('<input id="picFile" name="upload" type="file" style="width:260px;"/>');
            content.push('</div>');
            content.push('<div style="padding-left:20px; padding-top:10px;">');
            content.push('<input type="submit" name="Submit" value=" 上 傳 " />&nbsp;&nbsp;');
            content.push('<input type="reset" name="reset" value=" 重 置 " />');
            content.push('</div>');
            content.push('</form>');
            content.push("<iframe name='upload_frame' id='upload_frame' style='display:none' ></iframe>");
            content.push("</td></tr>");
            content.push("</table>");
               //彈窗口
            ymPrompt.win(content.join(''),300,200,'上傳文件');
           }
           catch(e){
            alert(e.message);  
           }
          }

          function callbackFun(flg,filepath){
           if(flg=='success'){
            var content=[];
            content.push("<span><img src='"+showPicpath+"/"+filepath+"' width='150' height='150' />");
            content.push("<input type='text' name='ad.file_path' value='"+filepath+"' readonly='true'/>");
            content.push("</span>");
            $("#pic_content").html(content.join(''));
            closeUploadPicWin();
           }
           else{
              alert("上傳圖片失敗");
           }
          }

          3
          <action name="imgUpload" class="imgAciton" method="execute">
             <interceptor-ref name ="fileUploadStack" />
                      <result name="showUpload">/jsp/advertisement/return.jsp</result>
            </action>

          4return.jsp

          <%@ page language="java"  pageEncoding="utf-8"%>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            <head> 
              <title>上傳成功</title>
            </head>
           
            <body>
           
            <%
          out.print("<script type='text/javascript'>parent.");
            %><s:property value='callbackName'/><%
          out.print("('success','"); 
            %><s:property value='imgFileName'/><%
          out.print("')</script>"); 
            %>

            </body>
          </html>


          5
          package com.gwtravel.action;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.util.Date;
          import java.util.Map;

          import javax.servlet.ServletConfig;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.apache.commons.io.FileUtils;
          import org.apache.log4j.Logger;
          import org.apache.struts2.ServletActionContext;
          import org.apache.struts2.interceptor.ServletRequestAware;
          import org.apache.struts2.interceptor.ServletResponseAware;
          import org.apache.struts2.interceptor.SessionAware;
          import org.springframework.context.annotation.Scope;
          import org.springframework.stereotype.Controller;
          import org.springframework.web.context.ServletConfigAware;

          import com.opensymphony.xwork2.ActionSupport;

          @Controller("imgAciton")
          @Scope("prototype")
          public class UplaodImg extends ActionSupport implements ServletConfigAware,
            ServletRequestAware, ServletResponseAware, SessionAware {
           static Logger logger = Logger.getLogger(UplaodImg.class);
           protected HttpServletRequest request;
           protected HttpServletResponse response;
           private ServletConfig servletConfig;
           Map sessionMap;
           private File upload; 
           private String uploadContextType;
           private String uploadFileName;
           
           private String imagePath;
           private String callbackName;
           private String imgFileName;//最后生成的文件名

           public File getUpload() {
            return upload;
           }

           public void setUpload(File upload) {
            this.upload = upload;
           }

           public String getUploadContextType() {
            return uploadContextType;
           }

           public void setUploadContextType(String uploadContextType) {
            this.uploadContextType = uploadContextType;
           }

           public String getUploadFileName() {
            return uploadFileName;
           }

           public void setUploadFileName(String uploadFileName) {
            this.uploadFileName = uploadFileName;
           }

           public String getImgFileName() {
            return imgFileName;
           }

           public void setImgFileName(String imgFileName) {
            this.imgFileName = imgFileName;
           }

           // 通過struts2的配置文件得到上傳目錄,這個是很重要的
           public String getImagePath() throws IOException {
            //判斷目錄是否存在,不存在則創建
            FileUtils.forceMkdir(new File(ServletActionContext.getRequest().getRealPath("/"+imagePath)));
            return ServletActionContext.getRequest().getRealPath("/"+imagePath);
           }

           public void setImagePath(String imagePath) {
            this.imagePath = imagePath;
           }

           public String getCallbackName() {
            return callbackName;
           }

           public void setCallbackName(String callbackName) {
            this.callbackName = callbackName;
           }

           private String getExtention(String fileName) {
                  int pos = fileName.lastIndexOf(".");
                  return fileName.substring(pos);
           }

           
           @Override
           public String execute() throws Exception {
            String imgName = new Date().getTime() + getExtention(uploadFileName);
          //  logger.error(getImagePath() + "/"+ imgName);
            System.out.println("test imgName: "+imgName);
            this.imgFileName = imgName;
            FileOutputStream fos = new FileOutputStream(getImagePath() + "/"+ imgName);
            FileInputStream fis = new FileInputStream(getUpload());
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
             fos.write(buffer, 0, len);
            }
            return "showUpload";
           }
           
           public String imgLogin1(){
             String imguser=request.getParameter("username");
             String imgpassword=request.getParameter("password");
             sessionMap.put("imguser", imguser);
             sessionMap.put("imgpassword", imgpassword);  
             return SUCCESS;
           }

           @Override
           public void setServletRequest(HttpServletRequest arg0) {
            this.request = arg0;

           }

           @Override
           public void setServletResponse(HttpServletResponse arg0) {
            // TODO Auto-generated method stub
            this.response = arg0;
           }

           @Override
           public void setSession(Map arg0) {
            this.sessionMap=arg0;

           }

           @Override
           public void setServletConfig(ServletConfig arg0) {
            // TODO Auto-generated method stub
            this.servletConfig = arg0;
           }
          }


          posted on 2012-03-15 16:49 計明敏 閱讀(613) 評論(0)  編輯  收藏 所屬分類: struts

          主站蜘蛛池模板: 左云县| 彭州市| 福鼎市| 瓮安县| 南部县| 周宁县| 邻水| 丰原市| 黄山市| 改则县| 卓尼县| 扬中市| 驻马店市| 庄浪县| 尼勒克县| 图木舒克市| 永定县| 镇平县| 太和县| 仪征市| 平定县| 皋兰县| 崇文区| 天水市| 大英县| 丹棱县| 海林市| 永寿县| 平湖市| 福贡县| 古田县| 江油市| 长治市| 竹山县| 谢通门县| 滦平县| 清水河县| 邹平县| 会同县| 东方市| 安宁市|