ExtJS中使用jspSmartUpload实现文件下载

项目需求:使用Ext的GridPanel实现文件下载功能!如[img]http://dl.iteye.com/upload/picture/pic/78068/fcc9a9e2-d4be-3961-a45b-a701bc00a1fa.jpg[/img]

首先,在gridpanel中加入链接style的button。在gridpanel的ColumnModel中
renderer:function(){return "<p><button type='submit' class='link' onclick='downloadfile("+v+")'><span>下载</span></button></p>"}

加入button style。
button.link {
font-family: "Verdana" sans-serif;
font-size: 1em;
text-align: left;
color: blue;
background: none;
margin: 0;
padding: 0;
border: none;
cursor: pointer;
}

其下载任务,都在downloadfile(v)这个方法中实现,代码
downloadfile = function(v){
Ext.Ajax.request({
url:"getFilePath_fileIO",
params:{"fileId":v},
success:function(response){
var fileJson = Ext.decode(response.responseText);

var filepath = encodeURIComponent(fileJson.filepath);
var filename = encodeURIComponent(fileJson.filename);
window.open("downloadfile.jsp?filepath="+filepath+"&filename="+filename,"_self");
},
failure:function(response){

}
});
}

在downloadfile.jsp页面中,使用jspSmartUpload提供的API实现下载。
	String filepath = request.getParameter("filepath");
String filename = request.getParameter("filename");
filepath = filepath.replace("/","\\");

SmartUpload su = new SmartUpload();
//初始化
su.init(config);
su.service(request,response);
//禁止浏览器自动打开文件
su.setContentDisposition(null);

//下载文件
String path = new String(filepath.getBytes("ISO8859-1"),"UTF-8");
String name = new String(filename.getBytes("ISO8859-1"),"UTF-8");
// System.out.print(name);
su.downloadFile(path,null,name);
out.clear();
out = pageContext.pushBody();


ps:jspSmartUpload在默认的情况,并不支持下载文件名为中文的。这里我们需要做一些编码的转换,在SmartUpload中加入toUtf8String(String)这个方法,如
  public static String toUtf8String(String s)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c >= 0) && (c <= 'ÿ')) {
sb.append(c); } else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
}
catch (Exception ex)
{
byte[] b;
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0)
k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}

修改SmartUpload中downloadFile(String s, String s1, String s2, int i)这个方法,如
public void downloadFile(String s, String s1, String s2, int i)
throws ServletException, IOException, SmartUploadException
{
if (s == null)
throw new IllegalArgumentException("File '" + s +
"' not found (1040).");
if (s.equals(""))
throw new IllegalArgumentException("File '" + s +
"' not found (1040).");
if ((!isVirtual(s)) && (this.m_denyPhysicalPath))
throw new SecurityException("Physical path is denied (1035).");
if (isVirtual(s))
s = this.m_application.getRealPath(s);
java.io.File file = new java.io.File(s);
FileInputStream fileinputstream = new FileInputStream(file);
long l = file.length();
boolean flag = false;
int k = 0;
byte[] abyte0 = new byte[i];
if (s1 == null)
this.m_response.setContentType("application/x-msdownload");
else if (s1.length() == 0)
this.m_response.setContentType("application/x-msdownload");
else
this.m_response.setContentType(s1);
this.m_response.setContentLength((int)l);
this.m_contentDisposition = (this.m_contentDisposition != null ? this.m_contentDisposition :
"attachment;");
if (s2 == null)
this.m_response.setHeader("Content-Disposition", this.m_contentDisposition +
" filename=" + toUtf8String(getFileName(s)));
else if (s2.length() == 0)
this.m_response.setHeader("Content-Disposition", this.m_contentDisposition);
else
this.m_response.setHeader("Content-Disposition", this.m_contentDisposition +
" filename=" + toUtf8String(s2));
while (k < l) {
int j = fileinputstream.read(abyte0, 0, i);
k += j;
this.m_response.getOutputStream().write(abyte0, 0, j);
}
fileinputstream.close();
}

这样,基本上能够实现对中文名文件的下载!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值