关于下载的一点心得!!(如何弹出对话框!)
注:inputStream不能在二个不同的类之间赋予.只能直接得到!!
FTP远程下载的例子:
//设置此项即告诉SERVLET是下载文件,而不是网页或TXT.(可以弹出对话框)
response.setContentType("APPLICATION/OCTET-STREAM");
//用IntputStream时,就用下面这个.
ServletOutputStream outputStream = response.getOutputStream();
//转换下载中文名的问题.
String fPath =new String(Filename.getBytes("GBK"), "ISO8859_1");
//设置下载的文件名与原文件名一样.
response.setHeader("Content-Disposition",
"attachment; filename=\"" + fPath +
"\"");
System.out.println(refFile.getFilename());
FTPClient ftpclient=new FTPClient();
//连接FTP服务器
ftpclient.connection(severurl)
if (refFile != null && (refFile.getFilesize() < 1024 * 1024 * 10)) {
temRemote=new String(("\\" + refFile.getFilename()).getBytes("gb2312"), "iso-8859-1");
inputStream = ftpclient.retrieveFileStream(temRemote);
System.out.println("00001");
}
else if (refFile != null && (refFile.getFilesize() > 1024 * 1024 * 10)) {
temRemote=new String((refFile.getMetaURL() + "\\" +
refFile.getFilename()).getBytes("gb2312"), "iso-8859-1");
inputStream = ftpclient.retrieveFileStream(temRemote);
}
System.out.println("<10M时=" + "\\" + refFile.getFilename());
System.out.println(">10M时=" + refFile.getMetaURL() + "\\" +
refFile.getFilename());
int i;
int chunk = inputStream.available();
System.out.println("chunk数据长度是=" + chunk);
//byte数组接受文件的数据
byte[] buffer = new byte[chunk];
int length = -1;
if (inputStream == null) {
System.out.println("输入流为空!!");
}
while((length=inputStream.read(buffer))!=-1){
System.out.println("length数据长度是=" + length);
outputStream.write(buffer, 0, length); //读入流,保存在BYTe数组中
}
inputStream.close();
outputStream.flush();
outputStream.close();
}