歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Applet 通過http上傳文件到服務器

Applet 通過http上傳文件到服務器

日期:2017/3/1 10:39:42   编辑:Linux編程
package com.cxsoft.applet;
import java.applet.Applet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/****************************************
* <p>Title:UploadFile </p> *
* <p>Description:上傳文件的applet </p> *
* <p>@author jerry </p> *
* <p>version: 1.0 </p> *
****************************************/

public class UploadFile extends Applet {

private static final long serialVersionUID = 1L;

public UploadFile() throws Exception {

HttpURLConnection con = this.getConnection();
con.disconnect();

//處理本地需要上傳的文件
String path = "D:\\EdmsScanDemo\\tm.zip";
File file = new File(path);
long fileLength = file.length(); // 獲取文件字節數
System.out.println("要上傳的文件大小為:" + fileLength);

//上傳文件到服務器
long startPos = this.getServletObject(con); //獲取服務器的返回信息

//判斷是否需要上傳
if(startPos < fileLength) {
System.err.println("文件未傳完,現在開始上傳剩余部分");
this.sendServletObject(con, path, startPos);
} else {
System.err.println("該文件已存在!");
}
}

/****************************************
* <p>功能:上傳文件到服務器 </p> *
* <p>@param con 與服務器的連接 </p> *
* <p>@param path 需要上傳的文件的路徑 </p> *
* <p>@throws IOException </p> *
****************************************/
private void sendServletObject(HttpURLConnection con, String path, long nstartPos)
throws Exception {

FileInputStream fis = null;
BufferedInputStream bis = null;
con = this.getConnection();

long startPos = nstartPos; //開始上傳的位置
BufferedOutputStream bos = null;
byte[] buf = new byte[4096]; //定義一個4M的緩存區
int len = 0;
//int totalBytes = 0;

try {
bos = new BufferedOutputStream(con.getOutputStream());
fis = new FileInputStream(path);
bis = new BufferedInputStream(fis);
bis.skip(startPos);

while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len); //上傳文件
bos.flush(); //緩存刷新
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
if (bos != null) {
bos.flush();
bos.close();
}
buf = null;
}
}

/********************************
* <p>功能:獲取與服務器的連接 </p> *
* <p>@return </p> *
* <p>@throws Exception </p> *
********************************/
private HttpURLConnection getConnection() throws Exception{
String url = "http://localhost:8080/UploadTest/temp";
String data = "test.zip";
URL server = new URL(url + "?filename=" + data);
// System.out.println("url= " + url + "?filename=" + data);

HttpURLConnection con = (HttpURLConnection) server.openConnection(); //獲取服務器端連接
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setDefaultUseCaches(false);

return con;
}

/********************************************
* <p>Title:getServletObject </p> *
* <p>Description:獲取服務器端的返回信息 </p> *
* <p>@param con </p> *
* <p>@throws IOException </p> *
********************************************/
private long getServletObject(HttpURLConnection con) throws Exception {

//向服務器端發送請求
ObjectOutputStream os = new ObjectOutputStream(con.getOutputStream());
os.write(1);
os.close();

//獲取服務器端的返回信息
ObjectInputStream ins = new ObjectInputStream(con.getInputStream());
String nStartPos = ins.readUTF();
System.out.println("已上傳的字節數為:" + nStartPos);
ins.close();

return Long.parseLong(nStartPos);
}

public static void main(String[] args) {
try {
UploadFile up = new UploadFile();
} catch (Exception e) {
e.printStackTrace();
}

}
}
Copyright © Linux教程網 All Rights Reserved