[code=Java][/code]/**
* 下载
*
* @return
*/
public String down() {
try {
filename=changechar(filename);
filenameweb=changechar(filenameweb);
String webPath="";
//真正用户名
String realFilename="";
String[] filenameArray=null;
if(filenameweb.trim().equalsIgnoreCase(""))
{
filenameArray=filename.split("[/]");
realFilename=filenameArray[filenameArray.length-1];
//取配置文件的webpath信息
InputStream is = getClass().getResourceAsStream("/db.properties");
Properties dbProps = new Properties();
dbProps.load(is);
// webPath = dbProps.getProperty("Titans.webPath", "");
dbProps = null;
filename="/"+filename;
}else
{
webPath=request.getRealPath("");
filenameArray=filenameweb.split("[/]");
realFilename=filenameArray[filenameArray.length-1];
filenameweb=webPath+"/"+filenameweb;
filename=filenameweb;
}
int i=65000;
java.io.File file = new java.io.File(filename);
java.io.FileInputStream fileinputstream = new java.io.FileInputStream(file);
long l = file.length();
if(l==0)
{
l = 1;
}
boolean flag = false;
int k = 0;
byte abyte0[] = new byte[i];
response.setContentType("application/x-msdownload");
String m_contentDisposition;
m_contentDisposition = new String();
m_contentDisposition=null;
response.setContentLength((int)l);
m_contentDisposition = m_contentDisposition != null ?
m_contentDisposition : "attachment;";
try
{
//取扩展名
String extFilename ="";
int a;
a=realFilename.lastIndexOf(".");
if (a>0)
{
extFilename=realFilename.substring(a+1).toUpperCase().trim();
}
//取主文件名
String mainFilename="";
int b;
b=realFilename.lastIndexOf(".");
if (b>0)
{
mainFilename=realFilename.substring(0,b).toUpperCase().trim();
}
realFilename=mainFilename.substring(0,16)+"."+extFilename;
realFilename=getFileName(filename);
}catch( Exception er)
{
}
response.setHeader("Content-Disposition", m_contentDisposition + " filename=" + ptoUtf8String(realFilename) );
while((long)k < l)
{
int j = fileinputstream.read(abyte0, 0, i);
k += j;
if(j==-1)
{
response.getOutputStream().write(abyte0, 0, 1);
k=1;
}
else
response.getOutputStream().write(abyte0, 0, j);
}
fileinputstream.close();
} catch (Exception e) {
ExceptionProcess ep = new ExceptionProcess();
ep.process(e, request);
log.error(e.getMessage());
}
finally
{
return null;
}
}
作者: sqh_me1232008
发布时间: 2010-09-29