js下载文件并修改文件名
用js下载文件,使用标签,添加download属性即可。
vara=document.createElement("a"); a.href="http://XXX.com/audioStream/8a9dbae9d0859e48fc1f590fcf6d4ccc.mp3"; a.download="test.mp3"; a.click();
但是如果想给文件重新命名,貌似js无法实现。
因此考虑后台实现,用java代理请求,获取文件设置文件名,返回到前端。
publicvoiddownFiles(HttpServletResponseresponse,Stringurl,StringworkInfoId,inttype){ try{ Stringprefix=type==1?"wav":"txt"; url=type==1?url:(url+"?textInfoId="+workInfoId); HttpEntityentity=Request.Get(url). execute().returnResponse().getEntity(); byte[]bys=EntityUtils.toByteArray(entity); //获取作品名称 Worksworks=this.worksDao.findByWorkId(workInfoId); Stringname=(works!=null&&StringUtils.isNotBlank(works.getName()))?works.getName():Long.toString(newDate().getTime()); response.setHeader("Content-Disposition","attachment;filename="+newString(name.getBytes("utf-8"),"ISO-8859-1")+"."+prefix); OutputStreamout=response.getOutputStream(); out.write(bys); out.close(); }catch(Exceptione){ e.printStackTrace(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。