Struts 上传下载

具体操作:

下载jsp页面:<td width="10%"><a href="${pageContext.request.contextPath}/control/DailyOffice/downFile.action?id=${st.id}"><img src="${pageContext.request.contextPath}/images/icons/page_white_text.png"/></a></td> 

上传JSP页面:<input name="image"  type="file"  class="Emingtextinput"/>      

操作:跳转到Action


Action类:

     private File image; //上传的文件
    private String imageFileName; //文件名称
    private String imageContentType; //文件类型
private Be_on_duty_Log blog = null;//日志model
private InputStream is = null;//文件流
private String filename;//文件名字


/**
* 下载文件struts交互类

* @return
* @throws Exception
*/
public InputStream getInputStream() throws Exception {
this.filename = blog.getAccName();


try {
this.filename = new String(this.filename.getBytes("GBK"),
"ISO8859_1");
is = new FileInputStream(blog.getAccUrl());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;


}
/**
* 下载文件管理
* @return
*/
public String  downFile() {
String id=request.getParameter("id");
Be_on_duty_Log logs=be_on_duty_LogDao.getLog(Integer.parseInt(id));
this.blog = logs;
return "download";
}

 

/**
* 添加日志
* @return
*/
public String addLog()  throws Exception{
String str=request.getParameter("image");
String root="D:\\Document\\dutyaccess\\";//存放附件的跟路径
/*--------拼凑文件名 start-------------*/
long timename=System.currentTimeMillis();//系统当前时间
String sufix=imageFileName.substring(imageFileName.lastIndexOf("."));//文件后缀
String fileName=String.valueOf(timename)+sufix;
/*--------拼凑文件名 end-------------*/
String saveUrl=root+fileName;//文件保存绝对路径


/*--------保存文件操作 start-------------*/
        if (image != null) {//保存文件操作
            File savefile = new File(new File(root),fileName);
            if (!savefile.getParentFile().exists())
                savefile.getParentFile().mkdirs();
            FileUtils.copyFile(image, savefile);
        }
        /*--------保存文件操作 end-------------*/
        
        /*--------保存数据库操作 start-------------*/
    Timestamp ts = new Timestamp(System.currentTimeMillis());//String 转Timestamp
String tsStr =(String)request.getParameter("addtm");//String 转Timestamp
ts = Timestamp.valueOf(tsStr);//String 转Timestamp

        Be_on_duty_Log beOnDutyLog=new Be_on_duty_Log();
        beOnDutyLog.setDutyId(Integer.parseInt((String)request.getParameter("currentid")));
        beOnDutyLog.setDutyContent(beOnDutyLogForm.getDutyContent());
        beOnDutyLog.setAddTm(ts);
        beOnDutyLog.setAccUrl(saveUrl);
        beOnDutyLog.setAccName(imageFileName);
        be_on_duty_LogDao.add(beOnDutyLog);
        /*--------保存数据库操作 end-------------*/
        
return "closeDialog";
}


struts下载核心配置文件:必须与这个  下载文件struts交互类 关联

<result name="current_showduty">/WEB-INF/page/Be_on_duty/current_showduty.jsp</result>
<result name="download" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">attachment;filename=${filename}</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">4096</param>
                </result>


其他资料:http://aimilin6688.iteye.com/blog/1062730

http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990674.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值