JavaWeb利用struts实现文件下载时改变文件名称
程序员文章站
2024-03-12 18:35:50
先看看效果图:
package com.fenghuo.struts.download;
import java.net.urlencoder;...
先看看效果图:
package com.fenghuo.struts.download; import java.net.urlencoder; import java.util.map; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.apache.struts.action.actionform; import org.apache.struts.action.actionforward; import org.apache.struts.action.actionmapping; import org.apache.struts.actions.downloadaction; import com.fenghuo.music.util.fileutils; public class download extends downloadaction { @override protected streaminfo getstreaminfo(actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception { //获取资源文件的名称 string resname = request.getparameter("resname"); //利用struts下载资源 return new downloadaction.resourcestreaminfo("audio/x-mpeg", this .getservlet().getservletcontext(), "/resource/music/" + resname); } @override public actionforward execute(actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception { //向浏览器发送消息头,使文件以附件的方式打开,并找到资源名对应的文件名称发送给浏览器 string resname = request.getparameter("resname"); fileutils fu = new fileutils(this.getservlet().getservletcontext()); map<string,string> musmap = fu.getmusmap(); string name = musmap.get(resname); //同时对文件名进行编码。在firefox下还是有乱码,不明白原因,望指教。 response.setheader("content-disposition", "attachment;filename=" + urlencoder.encode(name, "utf-8")); return super.execute(mapping, form, request, response); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java单例模式实现的几种方式