流的方式上傳文件

package com.baoyi.MyTest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class StreamTest {
 private InputStream in = null;
 private OutputStream out = null;
 private final static int BUFFER_SIZE = 16 * 1024;

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  File Infile = new File(
    "C:/Documents and Settings/All Users/Documents/My Pictures/213213.jpg");
  File Outfile = new File("D:/dd.jpg");
  InputStream in = null;
  OutputStream out = null;
  System.out.print(Infile.length());
  try {
   if (Infile.length() < 1024 * 1024 * 5) {
    System.out.print(Infile.length());
    in = new BufferedInputStream(new FileInputStream(Infile),
      BUFFER_SIZE);
    out = new BufferedOutputStream(new FileOutputStream(Outfile),
      BUFFER_SIZE);
    byte[] buffer = new byte[BUFFER_SIZE];
    while (in.read(buffer) > 0) {
     out.write(buffer);
    }
    System.out.print("圖片上傳!");
   } else {
    System.out.print("圖片沒有上傳!");
   }
  } catch (Exception e) {
   // TODO: handle exception
   System.out.print("圖片上傳出錯!" + e.getMessage());
  }
 }

}


發佈了57 篇原創文章 · 獲贊 16 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章