手把手教你上传女神照片到服务器
发布日期:2021-06-30 11:11:12 浏览次数:2 分类:技术文章

本文共 4473 字,大约阅读时间需要 14 分钟。



版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

概述

我们在开发过程中经常涉及到文件(图片、音频、压缩包等)的上传。绝大多数情况下,我们都采用第三方已经封装好的模块实现该功能。但是,它底层的实现原理是什么呢?我们可以自己动手实现文件的上传么?答案是肯定的!在此,我们使用Socket和IO流技术上传女神图片到服务器。

准备女神照片

在这里插入图片描述

景甜(Jing Tian),1988年7月21日出生于陕西省西安市,华语影视女演员。

2006年,景甜发行了个人首张音乐EP《你是谁》,并正式踏上演艺之路。

2010年,她凭借爱情片《我的美女老板》崭露头角 。
2011年,景甜因主演古装片《战国》而获得更多关注 。
2013年,其主演的《警察故事2013》 等三部电影合计票房超过12亿人民币 。
2014年,景甜主演了古装剧《班淑传奇》 。
2015年,其主演的传奇剧《大玉儿传奇》播出 。
2016年,她主演的魔幻片《长城》票房突破10亿人民币 。
2017年,景甜主演的宫廷剧《大唐荣耀》播出 。

客户端

package com.upload;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;/** * 本文作者:谷哥的小弟  * 博客地址:http://blog.csdn.net/lfdfhl */public class TCPUploadClient {
public static void main(String[] args) {
Socket socket=null; OutputStream outputStream=null; FileInputStream fileInputStream=null; InputStream inputStream=null; try {
//创建与服务器通信的Socket socket=new Socket(InetAddress.getLocalHost(), 10086); //获取Socket输出流 outputStream=socket.getOutputStream(); //指定待上传文件 fileInputStream=new FileInputStream("E:\\beauty.jpg"); //上传文件至服务端 byte[] b=new byte[1024*1]; int len=0; while((len=fileInputStream.read(b))!=-1) {
outputStream.write(b, 0, len); } //关闭客户端输出流 socket.shutdownOutput(); //读取服务端返回的消息 inputStream=socket.getInputStream(); byte[] buf=new byte[1024*1]; int length=0; while((length=inputStream.read(buf))!=-1) {
String string=new String(buf, 0, length); System.out.println(string); } } catch (Exception e) {
// TODO: handle exception }finally {
if(outputStream!=null) {
try {
outputStream.close(); } catch (IOException e) {
e.printStackTrace(); } } if(fileInputStream!=null) {
try {
fileInputStream.close(); } catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace(); } } if(inputStream!=null) {
try {
inputStream.close(); } catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null) {
try {
socket.close(); } catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace(); } } } }}

服务端

package com.upload;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;/** * 本文作者:谷哥的小弟  * 博客地址:http://blog.csdn.net/lfdfhl */public class TCPUploadServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(10086); while(true) {
Socket client=serverSocket.accept(); HandleUploadRunnableImpl handleUploadRunnableImpl=new HandleUploadRunnableImpl(client); Thread thread=new Thread(handleUploadRunnableImpl); thread.start(); } }}//处理上传文件的Runnableclass HandleUploadRunnableImpl implements Runnable{
private Socket socket=null; private InputStream inputStream=null; private FileOutputStream fileOutputStream=null; private OutputStream outputStream=null; public HandleUploadRunnableImpl(Socket socket) {
this.socket=socket; } @Override public void run() {
//获取客户端ip String ip=socket.getInetAddress().getHostAddress(); try {
//指定保存上传图片的目录 File uploadDir=new File("D:\\upload"); if(!uploadDir.exists()) {
uploadDir.mkdir(); } //指定文件保存的名称 File file=new File(uploadDir,ip+".jpg"); //读取客户端上传的文件并保存 inputStream=socket.getInputStream(); fileOutputStream=new FileOutputStream(file); byte[] b=new byte[1024*1]; int len=0; while((len=inputStream.read(b))!=-1) {
fileOutputStream.write(b, 0, len); } //向客服端返回消息 outputStream = socket.getOutputStream(); outputStream.write("上传完毕".getBytes()); } catch (Exception e) {
// TODO: handle exception }finally {
if(inputStream!=null) {
try {
inputStream.close(); } catch (IOException e) {
e.printStackTrace(); } } if(fileOutputStream!=null) {
try {
fileOutputStream.close(); } catch (IOException e) {
e.printStackTrace(); } } if(outputStream!=null) {
try {
outputStream.close(); } catch (IOException e) {
e.printStackTrace(); } } if(socket!=null) {
try {
socket.close(); } catch (IOException e) {
e.printStackTrace(); } } } } }

运行测试

在这里插入图片描述

转载地址:https://it9527.blog.csdn.net/article/details/104170707 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android利用Logcat监听应用程序本身被卸载
下一篇:Java实现多线程的三种方式

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月05日 21时14分15秒