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();
}
}
}
Applet 通过http上传文件到服务器
内容版权声明:除非注明,否则皆为本站原创文章。