首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2文件下传功能

2012-08-21 
struts2文件上传功能action代码如下package com.szhifly.railroadjx.product.actionimport java.util.Lis

struts2文件上传功能

action代码如下

package com.szhifly.railroadjx.product.action;import java.util.List;import org.apache.commons.io.FileUtils;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.apache.struts2.util.ServletContextAware;import com.szhifly.railroadjx.product.entity.ProductInfo;import com.szhifly.railroadjx.product.entity.ProductTemplet;import com.szhifly.railroadjx.common.BaseAction;import com.szhifly.railroadjx.common.ConditionData;import com.szhifly.railroadjx.common.DateUtils;import com.szhifly.railroadjx.common.Db;import com.szhifly.railroadjx.product.logic.interf.IProductInfoLogic;import com.szhifly.railroadjx.product.logic.interf.IProductTempletLogic;import java.io.*;import java.net.URLDecoder;import javax.servlet.ServletContext;  import java.text.DateFormat;   import java.text.SimpleDateFormat;   import java.util.Date;   import java.util.Random;public class test implements ServletContextAware {private static Logger log = Logger.getLogger(ProductInfoAction.class);//对应jsp页面中的<s:file name="doc" label="File" >nameprivate File doc;private String fileName;private String contentType;private ServletContext context;public void setDoc(File file) {this.doc = file;}public void setDocFileName(String fileName) {this.fileName = fileName;}public void setDocContentType(String contentType) {this.contentType = contentType;}public void setServletContext(ServletContext context) {this.context = context;}public ServletContext getContext() {return context;}public void setContext(ServletContext context) {this.context = context;}//上传文件public String uplaodFile()throws Exception { String targetDirectory="";String targetFileName="";targetDirectory="D:\\file";targetFileName = generateFileName(fileName);File target =new File(targetDirectory, targetFileName); FileUtils.copyFile(doc, target);return "success";}//获取某月所在的季度的所有月份private String getMonthFor3(int year,int month){int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};              int season = 1;              if(month>=1&&month<=3){                      season = 1;              }              if(month>=4&&month<=6){                      season = 2;              }              if(month>=7&&month<=9){                      season = 3;              }              if(month>=10&&month<=12){                      season = 4;              }          String returnString="";        for(int i=0;i<3;i++){        String tempMon=""+array[season-1][i];        if(tempMon.length()==1){        tempMon="0"+tempMon;        }        if("".equals(returnString)){        returnString+="'"+year+"-"+tempMon+"'";        }else{        returnString+=",'"+year+"-"+tempMon+"'";        }               }                    return returnString;  }//获取重新的名字private String generateFileName(String fileName) {int position = fileName.lastIndexOf(".");String extension = fileName.substring(position); //获取后缀String desfileName=DateUtils.getCurrentDate("yyyyMMddHHmmss")+"_"+(new Date()).getTime();return desfileName + extension;}}

?

jsp代码如下,form 必须加入enctype 这个属性,否则会失败的,doc 对应action的doc

<s:form name="createForm" action="/test.action" method="post"  enctype="multipart/form-data" ><s:file name="doc" label="File" ></s:file></s:form>

?

热点排行