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 计明敏 阅读(600) 评论(0)  编辑  收藏 所属分类: struts


只有注册用户登录后才能发表评论。


网站导航: