서버에 네트워크 프로그래밍 Socket 파일 업로드
3365 단어 소켓 통신
package com.softeem.scokt;
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketFileup
{
public static void fileup() {
ServerSocket ss = null;
DataOutputStream dout = null;
FileInputStream fis = null;
InputStream is = null;
BufferedInputStream bis = null;
RandomAccessFile rf = null;
File f = new File("f:/test/java .txt");
System.out.println(" 。。。");
try {
//
ss = new ServerSocket(8985);
//
Socket st = ss.accept();
//socket
is = st.getInputStream();
//
bis = new BufferedInputStream(is);
rf = new RandomAccessFile(f, "rw");
byte b[] = new byte[1024];
int len = 0;
//
while ((len = bis.read(b)) != -1) {
rf.write(b, 0, len);
rf.skipBytes(len);
}
System.out.println(" !");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
if (fis != null)
fis.close();
if (rf != null)
rf.close();
if (ss != null)
ss.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
fileup();
}
}
클라이언트:package com.softeem.scokt;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
public class SocketFileClient
{
public static void FileClient() {
Socket st = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
System.out.println(" 。。。");
try {
//
st = new Socket(InetAddress.getLocalHost(), 8985);
//
File f = new File("F:/a/a.txt");
bos = new BufferedOutputStream(st.getOutputStream());
fis = new FileInputStream(f);
int len = 0;
byte b[] = new byte[1024];
//
while ((len = fis.read(b)) != -1) {
bos.write(b, 0, len);
bos.flush();
}
System.out.println(" !");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
//
fis.close();
bos.close();
st.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
FileClient();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
안 드 로 이 드 블 루 투 스 학습 (2) 응용 층 블 루 투 스 장치 찾기, tcp 구현ip 통신cancelDiscovery (), 검색 과정 을 취소 하고 블 루 투 스 장치 검색 을 할 때 이 방법 을 사용 하면 검색 을 중단 합 니 다.(검색 과정 은 12 초 간 지속) disable () 블 루 투...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.