1.原理,浏览器访问-->localserver(下载文件)--文件又保存在另外一台服务器上。
即:在localserver上利用HttpURLConnection 先把文件读到localserver的流中,再从localserver输出到浏览器供下载。
localserver 的访问code:
String remoteUrl = reportRemoteIPUrl+"/projectname/xxxxServlet?sFileName="+sFileName; URL url = new URL(remoteUrl); URLConnection urlConnection = url.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; httpURLConnection.setDoOutput(true); //true -- will setting parameters httpURLConnection.setDoInput(true);//true--will allow read in from httpURLConnection httpURLConnection.setUseCaches(false);//will not use caches httpURLConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");//setting serialized java object. httpURLConnection.setRequestMethod("POST");//default is GET httpURLConnection.setRequestProperty("connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Charsert", "UTF-8"); httpURLConnection.setConnectTimeout(60000);//1 m httpURLConnection.setReadTimeout(60000);//1 m httpURLConnection.connect();//connect to server (tcp) OutputStream out = httpURLConnection.getOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(out); oout.writeObject(new String(sFileName));//write object oout.flush(); oout.close(); InputStream in = httpURLConnection.getInputStream();//send request to server long len = in.available(); response.reset(); if((contentType != null) && (len>0)){ response.setContentType(contentType); } int totalNum = 0; response.setHeader("Content-disposition", "attachment; filename=" + filename); //加上下面这句代码后,发现从localserver发送到浏览器的文件不完整。 //原因inputstream流中第一次接收不完整。 //response.setContentLength((int) len); response.setDateHeader("Expires", System.currentTimeMillis()); OutputStream sout = response.getOutputStream();//输出页面,以供下载 byte[] buffer = new byte[512]; int bufferSize; while ((bufferSize=in.read(buffer)) != -1 ) { sout.write(buffer,0,bufferSize); totalNum = totalNum + bufferSize; } sout.flush(); sout.close(); in.close();
2.remote server 接收请求后,再读取文件后,传给localserver。
InputStream inputStream = request.getInputStream();//用于接收post请求的输入流,可以从流中获取提交的数据 ObjectInputStream oinps = new ObjectInputStream(inputStream); Object path = oinps.readObject();//读取request数据,往往可以使post提交的参数 FileInputStream fis = new FileInputStream(path); InputStream fin = new BufferedInputStream(fis); //clear response response.reset(); response.setContentType(contentType); response.setHeader("Content-disposition", "attachment; filename=" + sFileName); response.setContentLength((int) len); response.setDateHeader("Expires", System.currentTimeMillis()); response.setContentType("application/octet-stream"); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[fis.available()]; int byteNum; int totalNum = 0; while ((byteNum = fin.read(buffer)) != -1) { toClient.write(buffer,0,byteNum); totalNum = totalNum + byteNum; } toClient.flush(); toClient.close(); fin.close();
相关推荐
1、利用HttpUrlConnection 代码如下:/** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exception */ public static String readParse(String urlPath) throws Exception { ...
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...
8.9 设置远程下载音乐为手机铃声——RingtoneManager与铃声存放路径 8.10 远程下载桌面背景图案——URLConnection与setWallpaper()搭配 8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动博客...
8.9 设置远程下载音乐为手机铃声——RingtoneManager与铃声存放路径 8.10 远程下载桌面背景图案——URLConnection与setWallpaper()搭配 8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动博客...
8.9 设置远程下载音乐为手机铃声——RingtoneManager与铃声存放路径 8.10 远程下载桌面背景图案——URLConnection与setWallpaper()搭配 8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动博客...
8.9 设置远程下载音乐为手机铃声——RingtoneManager与铃声存放路径 8.10 远程下载桌面背景图案——URLConnection与setWallpaper()搭配 8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动博客...
8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动博客发布器——以XML-RPC达成远程过程调用 8.13 移动RSS阅读器——利用SAXParser解析XML 8.14 远程下载安装Android程序——APKInstaller的...
8.9 设置远程下载音乐为手机铃声——RingtoneManager与铃声存放路径 8.10 远程下载桌面背景图案——URLConnection与setWallpaper()搭配 8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动...
8.9 设置远程下载音乐为手机铃声——RingtoneManager与铃声存放路径 8.10 远程下载桌面背景图案——URLConnection与setWallpaper()搭配 8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动博客...
8.9 设置远程下载音乐为手机铃声——RingtoneManager与铃声存放路径 8.10 远程下载桌面背景图案——URLConnection与setWallpaper()搭配 8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动博客...
8.9 设置远程下载音乐为手机铃声——RingtoneManager与铃声存放路径 8.10 远程下载桌面背景图案——URLConnection与setWallpaper()搭配 8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动博客...
8.9 设置远程下载音乐为手机铃声——RingtoneManager与铃声存放路径 8.10 远程下载桌面背景图案——URLConnection与setWallpaper()搭配 8.11 将手机文件上传至网站服务器——模拟HTTPFORM的POSTACTION 8.12 移动博客...
11.1.7 使用HttpURLConnection 11.1.8 使用AndroidHttpClient 11.1.9 使用后台线程(AsyncTask) 11.1.10 使用AsyncTask处理配置更改 11.1.11 使用DownloadManager获取文件 11.2 使用Android服务 ...
11.1.7 使用HttpURLConnection 11.1.8 使用AndroidHttpClient 11.1.9 使用后台线程(AsyncTask) 11.1.10 使用AsyncTask处理配置更改 11.1.11 使用DownloadManager获取文件 11.2 使用Android服务 ...