欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Struts2上传下载

程序员文章站 2023-01-28 13:56:34
jsp页面    

jsp页面
<body>
  <s:fielderror></s:fielderror>
  <s:form action="uploaduploadaction" enctype="multipart/form-data"
   theme="simple">
    用户名:<s:textfield name="username" />
   <br />
    密码: <s:textfield name="userpwd" />
   <br />
   <input type="file" name="file" />
   <br />
   <s:submit value="提交"></s:submit>
  </s:form>
  <br />
下载<a href="downloadaction">开始.gif</a>
 </body>
uploadaction
 


package com.hyl.action;

import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import org.apache.struts2.servletactioncontext;
import com.hyl.util.dateutil;
import com.opensymphony.xwork2.actionsupport;

public class uploadaction extends actionsupport {
 private file file;
 private string filefilename;
 private string filecontenttype;
 private string username;
 private string userpwd;

 public string upload() throws ioexception {
  string path = servletactioncontext.getrequest().getrealpath("/upload");
  // system.out.println(path);

  inputstream is = new fileinputstream(file);
  string date = dateutil.maildate(new java.util.date());
  // 截取的文件扩展名
  string fileextenname = filefilename
    .substring(filefilename.indexof('.'));
  // system.out.println("截取的文件扩展名"+filename);

  file serverfile = new file(path, date + fileextenname);

  outputstream os = new fileoutputstream(serverfile);

  byte[] b = new byte[1024];

  int length = 0;

  while ((length = is.read(b)) > 0) {
   os.write(b);

  }
  os.close();
  is.close();
  return success;
 }

 public file getfile() {
  return file;
 }

 public void setfile(file file) {
  this.file = file;
 }

 public string getfilefilename() {
  return filefilename;
 }

 public void setfilefilename(string filefilename) {
  this.filefilename = filefilename;
 }

 public string getfilecontenttype() {
  return filecontenttype;
 }

 public void setfilecontenttype(string filecontenttype) {
  this.filecontenttype = filecontenttype;
 }

 public string getusername() {
  return username;
 }

 public void setusername(string username) {
  this.username = username;
 }

 public string getuserpwd() {
  return userpwd;
 }

 public void setuserpwd(string userpwd) {
  this.userpwd = userpwd;
 }

}
downloadaction

 

package com.hyl.action;

import java.io.inputstream;
import java.io.unsupportedencodingexception;
import org.apache.struts2.servletactioncontext;
import com.opensymphony.xwork2.actionsupport;

public class downloadaction extends actionsupport {
//下面以中文名文件实例
 //此处文件名称由用户输入,此处也是动态传参的过程
 private string picname = "开始.gif";

 public inputstream getdownload() throws unsupportedencodingexception {
  //此处做一个中间变量,当重新编码后就无法识别中文名了
  string roursename=picname;
  //将源文件的中文名重新编码,目的值让struts的配置文件中能识别到,呈现给用户看
  picname=new string(picname.getbytes(),"iso-8859-1");
 
  system.out.println("/upload/"+roursename);
 
  return servletactioncontext.getservletcontext().getresourceasstream(
    "/upload/"+roursename);
 
 }

 public string execute() throws exception {

  return super.execute();
 }

 public string getpicname() {
  return picname;
 }

 public void setpicname(string picname) {
  this.picname = picname;
 }

}
dateutil

 

package com.hyl.util;

import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
import java.util.gregoriancalendar;

public class dateutil {

 public static string datetimechange(date source) {
  simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string changetime = format.format(source);
  return changetime;
 }

 public static string shortdate(date adate) {
  simpledateformat formatter = new simpledateformat("mm/dd/yyyy");
  return formatter.format(adate);
 }

 public static string nowdate() {
  string idate = "";
  simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
  string str = formatter.format(new date());
  string[] date = str.split("-");
  if (date.length >= 3) {
   idate = date[0] + "/" + date[1] + "/" + date[2] + "";
  } else {
   idate = str;
  }
  return idate;
 }

 public static string maildate(date adate) {
  simpledateformat formatter = new simpledateformat("yyyymmddhhmmsssss");
  return formatter.format(adate);
 }

 public static string dateparser(date adate) {
  simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
  return formatter.format(adate);
 }

 public static date parser(string strdate) {
  ;
  strdate = strdate.replace("/", "-");
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
  try {
   return sdf.parse(strdate);
  } catch (exception e) {
   return null;
  }
 }

 public static date parser(string strdate, string formatter) {
  simpledateformat sdf = new simpledateformat(formatter);
  try {
   return sdf.parse(strdate);
  } catch (exception e) {
   return null;
  }
 }

 public static string parser(date date, string formatter) {
  simpledateformat sdf = new simpledateformat(formatter);
  try {
   return sdf.format(date);
  } catch (exception e) {
   return null;
  }
 }

 public static date addmonth(date mydate, int amount) {
  gregoriancalendar cal = new gregoriancalendar();
  cal.settime(mydate);
  boolean isenddayofmonth_old = cal
    .getactualmaximum(gregoriancalendar.day_of_month) == cal
    .get(gregoriancalendar.day_of_month);
  cal.add(gregoriancalendar.month, amount);
  boolean isenddayofmonth_new = cal
    .getactualmaximum(gregoriancalendar.day_of_month) == cal
    .get(gregoriancalendar.day_of_month);
  if (isenddayofmonth_old && !isenddayofmonth_new) {
   cal.set(gregoriancalendar.date, cal
     .getactualmaximum(gregoriancalendar.day_of_month));
  }
  return cal.gettime();
 }

 public static date addday(date mydate, int amount) {
  calendar cal = calendar.getinstance();
  cal.settime(mydate);
  cal.add(calendar.day_of_month, amount);
  return cal.gettime();
 }

 public static date addminute(date mydate, int amount) {
  calendar cal = calendar.getinstance();
  cal.settime(mydate);
  int minute = 0;
  amount = -(amount);

  if (amount > 60) {

   int hour = (int) amount / 60;

   if (hour * 60 > amount) {
    minute = hour * 60 - amount;

    cal.add(calendar.hour_of_day, -hour);
    cal.add(calendar.minute, minute);

   } else if (hour * 60 < amount) {

    minute = amount - hour * 60;
    cal.add(calendar.hour_of_day, -hour);
    cal.add(calendar.minute, -minute);

   } else {
    cal.add(calendar.hour_of_day, -hour);
   }

  } else {

   cal.add(calendar.minute, -amount);
  }
  return cal.gettime();
 }

 public static date addyear(date mydate, int amount) {
  gregoriancalendar cal = new gregoriancalendar();
  cal.settime(mydate);
  boolean isenddayofmonth_old = cal
    .getactualmaximum(gregoriancalendar.day_of_month) == cal
    .get(gregoriancalendar.day_of_month);
  cal.add(gregoriancalendar.year, amount);
  boolean isenddayofmonth_new = cal
    .getactualmaximum(gregoriancalendar.day_of_month) == cal
    .get(gregoriancalendar.day_of_month);
  if (isenddayofmonth_old && !isenddayofmonth_new) {
   cal.set(gregoriancalendar.date, cal
     .getactualmaximum(gregoriancalendar.day_of_month));
  }
  return cal.gettime();
 }

 public static int getweekday(date mydate) {
  gregoriancalendar cal = new gregoriancalendar();
  cal.settime(mydate);
  return cal.get(gregoriancalendar.day_of_week);
 }

 public static int getconvertweekday(date mydate) {
  int day = getweekday(mydate);
  int result = day - 1;
  if (result == 0)
   result = 7;
  return result;
 }

 public static int gettimefromdate(date mydate) {
  simpledateformat sdf = new simpledateformat("hhmmss");
  int result = integer.parseint(sdf.format(mydate));
  return result;
 }

 public static long getdaysbetweendate(date startdate, date enddate) {
  calendar cal = calendar.getinstance();
  cal.settime(startdate);
  cal.set(calendar.hour, 0);
  cal.set(calendar.minute, 0);
  cal.set(calendar.second, 0);
  cal.set(calendar.millisecond, 0);
  startdate = cal.gettime();
  cal.settime(enddate);
  cal.set(calendar.hour, 0);
  cal.set(calendar.minute, 0);
  cal.set(calendar.second, 0);
  cal.set(calendar.millisecond, 0);
  return (cal.gettime().gettime() - startdate.gettime()) / 86400000;

 }

 public static string strdatetime(string str) {
  string idate = "";
  if (str != null) {
   string[] date = str.split("-");
   if (date.length >= 3) {
    idate = date[0] + "." + date[1] + "." + date[2];
   } else {
    idate = str;
   }
  }
  return idate;
 }

 public static string strdotdatetime(string str) {
  string idate = "";
  if (str != null) {
   string data0 = null;
   string[] date = str.split("-");
   if (date.length >= 3) {
    if (date[0] != null) {
     data0 = date[0].substring(2, 4);
    }
    idate = data0 + "." + date[1] + "." + date[2];
   } else {
    idate = str;
   }
  }
  return idate;
 }

 public static string bakdatetime(string str) {
  string idate = "";
  if (str != null) {
   int l1 = str.indexof(".");
   string d1 = str.substring(0, l1);
   string s1 = str.substring(l1 + 1);
   int l2 = s1.indexof(".");
   string d2 = s1.substring(0, l2);
   string d3 = s1.substring(l2 + 1);
   idate = d1 + "-" + d2 + "-" + d3;
  }
  return idate;
 }

 public static string strshortdatetime(string str) {
  string idate = "";
  if (str != null) {
   string[] date = str.split("-");
   if (date.length >= 3) {
    idate = date[0] + "." + date[1] + "." + date[2];
   } else {
    idate = str;
   }
   if (idate != null && idate.length() > 9) {
    idate = idate.substring(0, 10);

   }
  }
  return idate;
 }

 public static int getbetweendaynumber(string datea, string dateb) {
  long daynumber = 0;
  long day = 24l * 60l * 60l * 1000l;
  simpledateformat df = new simpledateformat("yyyy-mm-dd");
  try {
   java.util.date d1 = df.parse(datea);
   java.util.date d2 = df.parse(dateb);
   daynumber = (d2.gettime() - d1.gettime()) / day;
  } catch (exception e) {
   e.printstacktrace();
  }
  return (int) daynumber;
 }

 public static void main(string[] args) {
  system.out.println(nowdate());
 }
}
messagefile.properties

struts.messages.error.file.too.large=\u6587\u4ef6\u8fc7\u5927
struts.messages.error.content.type.not.allowed=\u6587\u4ef6\u7c7b\u578b\u4e0d\u4e00\u81f4

struts.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "">
<struts>
 <!--加载struts的消息资源文件   value里面指定messagefile.properties的文件名字,不写扩展名字-->
 <constant name="struts.custom.i18n.resources" value="messagefile"></constant>
 <package name="hyl" extends="struts-default">
  <!-- 上传的action -->
  <action name="*uploadaction" class="com.hyl.action.uploadaction"
   method="{1}">
   <result name="success">/ok.</result>
   <result name="input">/index.jsp</result>
   <interceptor-ref name="fileupload">
    <param name="allowedtypes">image/jpeg</param>
    <param name="maximumsize">102400</param>
   </interceptor-ref>
   <!-- 这个默认的拦截器必须放在自定义拦截器的下面,否则自定义拦截器不能呗调用 -->
   <interceptor-ref name="defaultstack"></interceptor-ref>
  </action>
  <!-- 下载的action -->
  <action name="downloadaction" class="com.hyl.action.downloadaction">
   <result name="success" type="stream">
    <!-- 这里显示的指定返回类型,像这种能被识别的类型会直接显示出来,
    如果是其他类型,会以下载形式出现,
    如果去掉这个标签,会启用默认的text/plain    
     -->  
    <param name="contenttype">image/jpeg</param>
    <!-- 此处的方法是action中的getdownload方法, -->
    <param name="inputname">download</param>
    <param name="contentdisposition">filename="${picname}"</param>
    <param name="buffersize">1024</param>
   </result>
  </action>

 </package>

</struts>   

 


作者:hyljava