import java.io.BufferedOutputStream;java import java.io.File;瀏覽器 import java.io.FileNotFoundException;app import java.io.IOException;dom import java.io.InputStream;spa import java.io.OutputStream;.net import java.io.RandomAccessFile;ci import java.net.HttpURLConnection;rem import java.net.URL;get import java.nio.MappedByteBuffer;input import java.nio.channels.FileChannel; import javax.servlet.http.HttpServletResponse; import com.hurong.credit.util.RsaHelper; public class DownloadURLFile { private static final DownloadURLFile downloadURLFile = new DownloadURLFile(); public static DownloadURLFile getInstance() { return downloadURLFile; } /** * 執行下載 * @param rfPosit 遠程文件開始下載位置 */ public static void start(long rfPosit,URL remoteFile,File storeFile){ HttpURLConnection httpConn = null; InputStream httpIn = null; try{ System.out.println("打開HttpURLConnection."); httpConn = (HttpURLConnection)remoteFile.openConnection(); httpConn.setRequestProperty("User-Agent","NetFox"); httpConn.setRequestProperty("RANGE","bytes="+rfPosit+"-"); System.out.println("獲得HttpInputStream."); httpIn = httpConn.getInputStream(); writeFile(httpIn,storeFile); System.out.println("關閉HttpURLConnection."); httpConn.disconnect(); }catch(Exception ex){ ex.printStackTrace(); } }
/** * 從HttpInputStream中讀數據並寫到本地文件中 * @param in HttpInputStream */ private static void writeFile(InputStream in,File storeFile){ RandomAccessFile fileOut = null; int buffer_len = 512; byte[] buffer = new byte[buffer_len]; int readLen = 0; try{ System.out.println("寫本地文件."); fileOut = new RandomAccessFile(storeFile, "rw"); fileOut.seek(fileOut.length()); while(-1 != (readLen = in.read(buffer, 0, buffer_len))){ fileOut.write(buffer, 0, readLen); } fileOut.close(); }catch(Exception ex){ ex.printStackTrace(); } }
/** * 大文件下載 * @param path * @param response */ public static void downLoad(String path, HttpServletResponse response){ final int BUFFER_SIZE = 0x3000;// 緩衝區大小爲3M boolean isInline = false; // 是否容許直接在瀏覽器內打開 OutputStream toClient = null; File f=new File(path);
/** * * map(FileChannel.MapMode mode,long position, long size) * * mode - 根據是按只讀、讀取/寫入或專用(寫入時拷貝)來映射文件,分別爲 FileChannel.MapMode 類中所定義的 * READ_ONLY、READ_WRITE 或 PRIVATE 之一 * * position - 文件中的位置,映射區域今後位置開始;必須爲非負數 * * size - 要映射的區域大小;必須爲非負數且不大於 Integer.MAX_VALUE * * 因此若想讀取文件後半部份內容,如例子所寫;若想讀取文本後1/8內容,須要這樣寫map(FileChannel.MapMode.READ_ONLY, * f.length()*7/8,f.length()/8) * * 想讀取文件全部內容,須要這樣寫map(FileChannel.MapMode.READ_ONLY, 0,f.length()) * */
MappedByteBuffer inputBuffer=null; long start = 0; try { String filename = new String(f.getName().getBytes(), "ISO8859-1"); String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); response.reset(); String inlineType = isInline ? "inline" : "attachment"; // 是否內聯附件 response.setHeader("content-disposition", "attachment;filename=\""+ filename + "\""); //response.setContentType("application/pdf"); inputBuffer = new RandomAccessFile(f, "r").getChannel().map(FileChannel.MapMode.READ_ONLY, 0,f.length()); byte[] dst = new byte[BUFFER_SIZE];// 每次讀出3M的內容 start = System.currentTimeMillis(); // if(inputBuffer.capacity()>BUFFER_SIZE){ for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) { if (inputBuffer.capacity() - offset >= BUFFER_SIZE) { for (int i = 0; i < BUFFER_SIZE; i++) dst[i] = inputBuffer.get(offset + i); } else {
for (int i = 0; i < inputBuffer.capacity() - offset; i++)
dst[i] = inputBuffer.get(offset + i); } toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream;charset=gbk"); toClient.write(dst); } /*}else{ //dst[0]=inputBuffer; toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream;charset=gbk"); toClient.write(inputBuffer.get(0)); }*/ } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != toClient) { toClient.close(); toClient.flush(); } } catch (Exception e) { e.printStackTrace(); } } long end = System.currentTimeMillis(); System.out.println("讀取文件文件內容花費:" + (end - start) + "毫秒"); } } |