day25
文章目录
一、sockt传送图片
Java在服务器端和客户端传输图片(一个程序只能传一次)
Java在服务器端和客户端传输图片(一个程序可以传多次)
二、Java Applet 程序
一、sockt传送图片
Java在服务器端和客户端传输图片(一个程序只能传一次)
实例如下:
服务器端
package Demo01FileUpload;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClient {
public static void main(String[] args) throws UnknownHostException, IOException {
FileInputStream fis= new FileInputStream(“D:\SGZ\DAY25\01-端口号.bmp”);
Socket socket =new Socket(“127.0.0.1”,8888);
OutputStream os=socket.getOutputStream();
int len=0;
byte[] bytes= new byte[1024];
while((len=fis.read(bytes))!=-1) {
os.write(bytes,0,len);
}
socket.shutdownOutput();
InputStream is=socket.getInputStream();
while((len=is.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
fis.close();
socket.close();
}
}
客户端:
package Demo01FileUpload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket server= new ServerSocket(8888);
Socket socket=server.accept();
InputStream is=socket.getInputStream();
File file= new File(“D:\SGZ\2020080605050”);
if(!file.exists()) {
file.mkdirs();
}
FileOutputStream fos= new FileOutputStream(file+"\1.bmp");
int len=0;
byte[] bytes= new byte[1024];
while((len=is.read(bytes))!=-1) {
fos.write(bytes,0,len);
}
socket.getOutputStream().write("图片上传成功".getBytes());
fos.close();
socket.close();
server.close();
}
}
服务器端:
package Demo02FileUpload;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClient {
public static void main(String[] args) throws UnknownHostException, IOException {
FileInputStream fis= new FileInputStream(“D:\SGZ\DAY25\01-端口号.bmp”);
Socket socket =new Socket(“127.0.0.1”,8888);
OutputStream os=socket.getOutputStream();
int len=0;
byte[] bytes= new byte[1024];
while((len=fis.read(bytes))!=-1) {
os.write(bytes,0,len);
}
socket.shutdownOutput();
InputStream is=socket.getInputStream();
while((len=is.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
fis.close();
socket.close();
}
}
客户端:
package Demo02FileUpload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class TcpServer {
public static void main(String[] args) {
ServerSocket server= new ServerSocket(8888);
while(true) {
Socket socket=server.accept();
new Thread (new Runnable() {
public void run() {
try {
InputStream is=socket.getInputStream();
File file= new File(“D:\\SGZ\\2020080605050”);
if(!file.exists()) {
file.mkdirs();
}
String fileName=“classFive”+System.currentTimeMillis()+new Random().nextInt(99999)+".bmp";
FileOutputStream fos =new FileOutputStream(file+"\"+fileName);
int len=0;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))!=-1) {
fos.write(bytes,0,len);
}
socket.getOutputStream().write(“图片上传成功”.getBytes());
fos.close();
socket.close();
}catch(IOException e) {
System.out.println(e);
}
}
}
}
}
}
二、Java Applet 程序
Java小应用程序(Java Applet)使用Java语言编程的一段代码,它嵌入HTML文档中,通过网络传输并运行在浏览器环境下。它的执行方式与一般应用程序不同,生命周期较为复杂。