1.通过socket链接就不用说了,网上例子很多.2.通过http协议链接,链接结果保存到文件中.import java.io.BufferedOutputStream;
import java.io.*;
import java.net.*;
import java.net.URLConnection;public class TestServlet {
public TestServlet() {
try {
StringBuffer strFileInfoBuf = new StringBuffer(); String strProcessURL =
"http://127.0.0.1/NSSB/MainServletXGMCX"
+ "?FLAG=sendfile&name=myname"; //connect server
URL objURL = new URL(strProcessURL);
//open connect
URLConnection objConn = objURL.openConnection();
objConn.setDoOutput(true);
//get server return message
// BufferedInputStream objInput = new BufferedInputStream(objConn.getInputStream());
StringBuffer strTempBuf = new StringBuffer();
byte[] byteArray = new byte[2048];
int nReadCount = -1;
BufferedOutputStream objOutput =
new BufferedOutputStream(objConn.getOutputStream());
FileInputStream objFileIn = new FileInputStream("c:\\test.txt");
while ((nReadCount = objFileIn.read(byteArray)) != -1) {
objOutput.write(byteArray, 0, nReadCount);
}
objOutput.flush();
objFileIn.close();
objOutput.close(); String strServerInfo = strTempBuf.toString();
} catch (Exception e) {
e.printStackTrace();
}
} public void testServletPost(){
try {
StringBuffer strFileInfoBuf = new StringBuffer(); String strProcessURL =
"http://127.0.0.1/NSSB/MainServletXGMCX"
+ "?FLAG=sendfile&name=myname"; //connect server
URL objURL = new URL(strProcessURL);
//open connect
HttpURLConnection objConn = (HttpURLConnection) objURL.openConnection();
//设置允许output
objConn.setDoOutput(true);
//设置为post方式
objConn.setRequestMethod("POST");
objConn.setUseCaches(false);
objConn.setRequestProperty("enctype", "multipart/form-data");
objConn.setRequestProperty("contentType", "charset=GBK"); //get server return message
// BufferedInputStream objInput = new BufferedInputStream(objConn.getInputStream());
StringBuffer strTempBuf = new StringBuffer();
byte[] byteArray = new byte[2048];
int nReadCount = -1;
BufferedOutputStream objOutput =
new BufferedOutputStream(objConn.getOutputStream());
FileInputStream objFileIn = new FileInputStream("c:\\test.txt");
while ((nReadCount = objFileIn.read(byteArray)) != -1) {
objOutput.write(byteArray, 0, nReadCount);
}
objOutput.flush();
objFileIn.close();
objOutput.close(); String strServerInfo = strTempBuf.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TestServlet testServlet1 = new TestServlet();
}
}
import java.io.*;
import java.net.*;
import java.net.URLConnection;public class TestServlet {
public TestServlet() {
try {
StringBuffer strFileInfoBuf = new StringBuffer(); String strProcessURL =
"http://127.0.0.1/NSSB/MainServletXGMCX"
+ "?FLAG=sendfile&name=myname"; //connect server
URL objURL = new URL(strProcessURL);
//open connect
URLConnection objConn = objURL.openConnection();
objConn.setDoOutput(true);
//get server return message
// BufferedInputStream objInput = new BufferedInputStream(objConn.getInputStream());
StringBuffer strTempBuf = new StringBuffer();
byte[] byteArray = new byte[2048];
int nReadCount = -1;
BufferedOutputStream objOutput =
new BufferedOutputStream(objConn.getOutputStream());
FileInputStream objFileIn = new FileInputStream("c:\\test.txt");
while ((nReadCount = objFileIn.read(byteArray)) != -1) {
objOutput.write(byteArray, 0, nReadCount);
}
objOutput.flush();
objFileIn.close();
objOutput.close(); String strServerInfo = strTempBuf.toString();
} catch (Exception e) {
e.printStackTrace();
}
} public void testServletPost(){
try {
StringBuffer strFileInfoBuf = new StringBuffer(); String strProcessURL =
"http://127.0.0.1/NSSB/MainServletXGMCX"
+ "?FLAG=sendfile&name=myname"; //connect server
URL objURL = new URL(strProcessURL);
//open connect
HttpURLConnection objConn = (HttpURLConnection) objURL.openConnection();
//设置允许output
objConn.setDoOutput(true);
//设置为post方式
objConn.setRequestMethod("POST");
objConn.setUseCaches(false);
objConn.setRequestProperty("enctype", "multipart/form-data");
objConn.setRequestProperty("contentType", "charset=GBK"); //get server return message
// BufferedInputStream objInput = new BufferedInputStream(objConn.getInputStream());
StringBuffer strTempBuf = new StringBuffer();
byte[] byteArray = new byte[2048];
int nReadCount = -1;
BufferedOutputStream objOutput =
new BufferedOutputStream(objConn.getOutputStream());
FileInputStream objFileIn = new FileInputStream("c:\\test.txt");
while ((nReadCount = objFileIn.read(byteArray)) != -1) {
objOutput.write(byteArray, 0, nReadCount);
}
objOutput.flush();
objFileIn.close();
objOutput.close(); String strServerInfo = strTempBuf.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TestServlet testServlet1 = new TestServlet();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货