服务器上传文件java代码简单实现

将文件(图片)上传至服务器

 

客户端代码实现

package com.company.demo20FileUp;

import java.io.*;
import java.net.Socket;

/*
 * 文件上传客户端实现
 * */
public class TCPClient {
    public static void main(String[] args) throws IOException {

//        1、创建一个本地字节输入流FileInputStream对象,构造方法中绑定要读取的数据源。
        FileInputStream fis= new FileInputStream("f:\\file\\timg.jpg");

//        2、创建一个客户端socker对象,构造方法中绑定服务器的IP地址和端口号
        Socket socket = new Socket("127.0.0.1",8888);

//        3、使用Socket中的方法getOutputStream,获取网络字节输出流outputStream对象。
        OutputStream os = socket.getOutputStream();

//        4、使用本地字节输入流FileInputStream对象中的方法read,读取本地文件
        int len = 0 ;
        byte[] bytes=new byte[1024];
        while ((len = fis.read(bytes))!= -1){

//            5、使用网络字节输出刘OutputStream对象中的方法write,把读取到的文件上传到服务器。
        os.write(bytes,0,len);
        }
        socket.shutdownOutput();
//        6、使用socket中的方法getinputStream,获取网络字节输入流Input Stream对象。
        InputStream is = socket.getInputStream();

//        7、使用网络字节输入流inputStream对象中的方法read读取服务回传的数据
        while ((len = is.read(bytes))!= -1){

            System.out.println(new String(bytes,0,len));

        }

//        8、释放资源
        fis.close();
        socket.close();





    }

}

 

服务器端代码实现

package com.company.demo20FileUp;

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 {

//        1、创建一个服务器的serverSocket对象,和系统要指定的端口号
        ServerSocket server = new ServerSocket(8888);

//        2、使用ServerSocket对象中的方法accept,获取请求到客户端的Socket对象
        Socket socket = server.accept();

//        3、使用sockt对象中的方法getInputSream,获取到网络字节输入
        InputStream is = socket.getInputStream();

//        4、判断文件夹是否存在
        File file = new File("f:\\file1");
        if(!file.exists()){
            file.mkdir();//没有的话就创建
        }

//        5、创建一个本地字节流FileOutputStream对象,构造方法中绑定输出目的地址;
        FileOutputStream fos = new FileOutputStream(file+"\\2.jpg");

//        6、使用网络字节输入流InputSream对象方法中的read,读取客户端上传的文件
        int len = 0 ;
        byte[] bytes=new byte[1024];
        while ((len = is.read(bytes))!= -1){

//            7、使用本地字节输出流FileOutputSream对象中的方法writer,把读取的文件放到硬盘上
            fos.write(bytes,0,len);
        }


//        8、使用socket对象中的方法getOutputStream,获取到网络字节输出流OutputStream对象;
//        9、使用OutputStream对象中的方法writer,给客户端回传
        socket.getOutputStream().write("上传成功".getBytes());

//        10、释放资源
        fos.close();
        socket.close();
        server.close();


    }

}

最后实现效果图

客户端:

 

服务器端:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单上传文件JAVA代码实现: ```java import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class FileUploader { public static void uploadFile(String serverUrl, String filePath) throws IOException { File file = new File(filePath); URL url = new URL(serverUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/octet-stream"); connection.setRequestProperty("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); OutputStream outputStream = connection.getOutputStream(); InputStream inputStream = file.toURI().toURL().openStream(); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); outputStream.close(); inputStream.close(); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { System.out.println("File uploaded successfully."); } else { System.out.println("File upload failed. Response code: " + responseCode); } } } ``` 你可以调用 `uploadFile` 方法来上传文件,例如: ```java FileUploader.uploadFile("http://example.com/upload", "/path/to/file"); ``` 这将会把 `/path/to/file` 文件上传到 `http://example.com/upload` 服务器上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值