本文目錄一覽:
- 1、java編程:我在做一個上傳文件的下載,下載方法返回的是InputStream類型,這樣的話如果文件存在,點擊
- 2、java數據庫blob字段的下載(讀取)
- 3、用java程序來做限速,下載文件的代碼修改
- 4、java不在磁盤創建文件,直接返迴流給用戶下載
- 5、java下載功能實現
- 6、求問Java文件下載的幾種方式
java編程:我在做一個上傳文件的下載,下載方法返回的是InputStream類型,這樣的話如果文件存在,點擊
無論是servlet還是springmvc,一般的做法是先用一個servlet或method處理數據,如果正常才會跳轉到下載用的servlet或method。 如果不正常,直接轉錯誤提示畫面就好了。
java數據庫blob字段的下載(讀取)
這是我以前寫的代碼,放在action里。圖片在pojo類中對應為byte[]類型,clxxb是一個pojo類,clxxb.getClpic()得到圖片對應的位元組數組byte[]。其實輸出文件就是輸出一個位元組流。希望對你有幫助。
InputStream input=clxxb.getClpic().getBinaryStream();
byte[] buffer=new byte[input.available()];
ServletOutputStream out=response.getOutputStream();
int length=0;
while((length=input.read(buffer))!=-1){
out.write(buffer,0,length);
}
out.flush();
out.close();
用java程序來做限速,下載文件的代碼修改
用java程序來做限速,方向就有問題,如果大並發量,都要控制速度你程序早就掛了。應該用應用服務器(我想你用的tomcat吧)+http服務器控制,這裡用apache就可以了。用apache來做限速才是正確的,用程序代碼幾乎是沒法限速,http 協議是無狀態的協議。
當然你一定要做, 那就是寫一定數量流,拿這個數據流的大小與你限速的平均量比,如果超過了就程序暫停一會。(這樣設計很不合理,但可以達到你的要求)
還有兩個,快了
java不在磁盤創建文件,直接返迴流給用戶下載
沒懂你的意思,用戶要下載的東西是什麼?不是在磁盤上的東西么?還是下載的是程序自己生成的數據?如果是程序自己生成的數據,那就更簡單了呀~建議用java.nio來做,先生成把要傳的數據寫入緩衝區,再將緩衝區的數據寫入通道.我還沒懂你的提問是什麼意思?所以沒辦法給你寫例子,我發一段考貝文件的例子給你
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class CopyFile {
public static void main(String[] args) {
ByteBuffer bb=null;
try {
// FileOutputStream fileOut=new FileOutputStream(“f:\\1.txt”);
// FileInputStream fileIn=new FileInputStream(“f:\\2.txt”);
RandomAccessFile raf=new RandomAccessFile(“f://1.txt”,”rw”);
RandomAccessFile rafo=new RandomAccessFile(“f://2.txt”,”rw”);
//獲得寫入和讀取通道
FileChannel foc=raf.getChannel();
FileChannel fic=rafo.getChannel();
//分配緩衝區大小
bb=ByteBuffer.allocate(1024);
//將數據讀入緩衝區
foc.read(bb);
bb.put(“還可以加入數據”.getBytes());
//反轉數據,反轉以後,不能再放數據
bb.flip();
fic.write(bb);
} catch (Exception e) {
}finally{
bb.clear();
}
}
}
java下載功能實現
樓主得在後台的控制器中用reponse的輸出流轉化一下,我給你個例子。
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();response.addHeader(“Content-Disposition”, “attachment;filename=” + new String(fileName.getBytes(“gbk”),”ISO-8859-1″));response.addHeader(“Content-Length”, “” + excelFile.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType(“application/octet-stream”);toClient.write(buffer);toClient.flush();toClient.close();
求採納為滿意回答。
求問Java文件下載的幾種方式
InputStream fis = new BufferedInputStream(new FileInputStream(path));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();// 設置response的Headerresponse.addHeader(“Content-Disposition”, “attachment;filename=” + new String(filename.getBytes()));response.addHeader(“Content-Length”, “” + file.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType(“application/octet-stream”);toClient.write(buffer);toClient.flush();toClient.close();} catch (IOException ex) {ex.printStackTrace();}return response;}public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {// 下載本地文件String fileName = “Operator.doc”.toString(); // 文件的默認保存名// 讀到流中InputStream inStream = new FileInputStream(“c:/Operator.doc”);// 文件的存放路徑// 設置輸出的格式response.reset();response.setContentType(“bin”);response.addHeader(“Content-Disposition”, “attachment; filename=\”” + fileName + “\””);// 循環取出流中的數據byte[] b = new byte[100];int len;try {while ((len = inStream.read(b)) 0)response.getOutputStream().write(b, 0, len);inStream.close();} catch (IOException e) {e.printStackTrace();}}public void downloadNet(HttpServletResponse response) throws MalformedURLException {// 下載網絡文件int bytesum = 0;int byteread = 0;URL url = new URL(“windine.blogdriver.com/logo.gif”);try {URLConnection conn = url.openConnection();InputStream inStream = conn.getInputStream();FileOutputStream fs = new FileOutputStream(“c:/abc.gif”);byte[] buffer = new byte[1204];int length;while ((byteread = inStream.read(buffer)) != -1) {bytesum += byteread;System.out.println(bytesum);fs.write(buffer, 0, byteread);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}} //支持在線打開文件的一種方式public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {File f = new File(filePath);if (!f.exists()) {response.sendError(404, “File not found!”);return;}BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));byte[] buf = new byte[1024];int len = 0;response.reset(); // 非常重要if (isOnLine) { // 在線打開方式URL u = new URL(“” + filePath);response.setContentType(u.openConnection().getContentType());response.setHeader(“Content-Disposition”, “inline; filename=” + f.getName());// 文件名應該編碼成UTF-8} else { // 純下載方式response.setContentType(“application/x-msdownload”);response.setHeader(“Content-Disposition”, “attachment; filename=” + f.getName());}OutputStream out = response.getOutputStream();while ((len = br.read(buf)) 0)out.write(buf, 0, len);br.close();out.close();}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/306448.html