第七次java实验
获取网址的前端代码:
public class Text1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
URL sina=new URL("http://www.cqust.com");
BufferedReader in=new BufferedReader(new InputStreamReader(sina.openStream()));
String inputLine;
while((inputLine=in.readLine())!=null)
System.out.println(inputLine);
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
从网络中获取图片,上传到本地指定目录代码实现
public class text2 {
public static void download(String urlString, int i) throws Exception { //创建方法,实现上传的路径,和保存文件名称。
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
String filename = "D:\\图片下载/" + i + ".jpg"; //下载路径及下载图片名称
File file = new File(filename);
FileOutputStream os = new FileOutputStream(file, true);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
System.out.println(i);
// 完毕,关闭所有链接
os.close();
is.close();
}
public static void main(String[] args) {
try {
download("http://www.cqust.edu.cn/images/logo.png", 2); //输入获取到的文件图片路径,和需要保存的文件名。
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
创建一个服务器Server,客户端可以上传图片到Server中,Server进行图片保存S
Server端代码
/*
实现步骤:
1.创建一个服务器服务器套接字对象,和系统要指定的端口号
2.使用ServerSocket对象中的方法accept,获取到请求的客户端Socket对象
3.使用socket对象中的方法getInputStream,获取到网络字节输入流InputStream对象4.判断d : llupload文件夹是否存在,不存在则创建
5.创建一个本地字节输出流文件输出流对象,构造方法中绑定要输出的目的地6。使用网络字节输入流Inputstream对象中的方法Read,读取客户端上传的文件
7.使用本地字节输出流FileoutputStream对象中的方法Way,把读取到的文件保存到服务器的硬盘上8。使用Socket对象中的方法getoutputStream,获取到网络字节输出流outputStream对象
9.使用网络字节输出流outputstream对象中的方法Way,给客户端回写“上传成功”
10.释放资源(FiLeoutputstream, Socket, serversocke
*/
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8888);
//让服务器一直处于监听状态
while(true){
Socket socket = server.accept();
//采用多线程的技术,提高程序的效率
new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream is = socket.getInputStream();
File file = new File("F:\\javaTpian\\fuwu");
if (!file.exists()){
file.mkdirs();
}
String filename = "itcast"+System.currentTimeMillis()+new Random().nextInt(999999)+".jpg";
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);
}
}
}).start();
}
}
}
客户端Client
/*
文件上传案例的客户端:读取本地文件,上传到服务器,读取服务器回写的数据明确:
数据源:C:!11.jpg自的地:服务器实现步骤;
1.创建一个本地字节输入流FileInputstream对象,构造方法中绑定要读取的数据源2。创建一个客户端套接字对象,构造方法中绑定服务器的IP地址和端口号
2.创建一个客户端套接字对象,构造方法中绑定服务器的IP地址和端口号
3.使用Socket中的方法getoutputStream,获取网络字节输出流outputstream对象4。使用本地字节输入流FileInputstream对象中的方法Read,读取本地文件
4.使用本地字节输入流FileInputStream对象中的方法Read,读取本地文件
5.使用网络字节输出流outputstream对象中的方法Way,把读取到的文件上传到服务器6。使用Socket中的方法getInputStream,获取网络字节输入流Inputstream对象
6.使用Socket中的方法getInputStream,获取网络字节输入流Inputstream对象
7.使用网络字节输入流Inputstream对象中的方法readi读取服务回写的数据
8.释放资源(FileinputStream,Socket)
*/
import java.io.*;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) throws IOException {
// 1.创建一个本地字节输入流FileInputstream对象,构造方法中绑定要读取的数据源2。创建一个客户端套接字对象,
// 构造方法中绑定服务器的IP地址和端口号
FileInputStream fis = new FileInputStream("F:\\javaTpian\\xy.jpg");
// 2.创建一个客户端套接字对象,构造方法中绑定服务器的IP地址和端口号
Socket socket = new Socket("127.0.0.1", 8888);
// 3.使用Socket中的方法getoutputStream,获取网络字节输出流outputstream对象4。使用本地字节输入流
// FileInputstream对象中的方法Read,读取本地文件
OutputStream os = socket.getOutputStream();
// 4.使用本地字节输入流FileInputStream对象中的方法Read,读取本地文件
int len = 0;
byte[] bytes = new byte[1024];
while((len = fis.read(bytes))!=-1){
// 5.使用网络字节输出流outputstream对象中的方法write,把读取到的文件上传到服务器。使用Socket中
// 的方法getInputStream,获取网络字节输入流Inputstream对象
os.write(bytes,0,len);
}
//结束标记
socket.shutdownOutput();
// 6.使用Socket中的方法getInputStream,获取网络字节输入流Inputstream对象
InputStream is = socket.getInputStream();
// 7.使用网络字节输入流Inputstream对象中的方法readi读取服务回写的数据
while((len = fis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
// 8.释放资源(FileinputStream,Socket)
fis.close();
socket.close();
}
}