java中如何实现从客户端发送文件到服务器端?

Python012

java中如何实现从客户端发送文件到服务器端?,第1张

服务器端源码:\x0d\x0aimport java.io.BufferedReader\x0d\x0aimport java.io.File\x0d\x0aimport java.io.FileNotFoundException\x0d\x0aimport java.io.FileOutputStream\x0d\x0aimport java.io.IOException\x0d\x0aimport java.io.InputStream\x0d\x0aimport java.io.InputStreamReader\x0d\x0aimport java.net.ServerSocket\x0d\x0aimport java.net.Socket\x0d\x0a\x0d\x0a/**\x0d\x0a *\x0d\x0a * 文件名:ServerReceive.java\x0d\x0a * 实现功能:作为服务器接收客户端发送的文件\x0d\x0a *\x0d\x0a * 具体实现过程:\x0d\x0a * 1、建立SocketServer,等待客户端的连接\x0d\x0a * 2、当有客户端连接的时候,按照双方的约定,这时要读取一行数据\x0d\x0a * 其中保存客户端要发送的文件名和文件大小信息\x0d\x0a * 3、根据文件名在本地创建文件,并建立好流通信\x0d\x0a * 4、循环接收数据包,将数据包写入文件\x0d\x0a * 5、当接收数据的长度等于提前文件发过来的文件长度,即表示文件接收完毕,关闭文件\x0d\x0a * 6、文件接收工作结束\x0d\x0a\x0d\x0apublic class ServerReceive {\x0d\x0a \x0d\x0apublic static void main(String[] args) {\x0d\x0a \x0d\x0a/**与服务器建立连接的通信句柄*/\x0d\x0aServerSocket ss = null\x0d\x0aSocket s = null\x0d\x0a \x0d\x0a/**定义用于在接收后在本地创建的文件对象和文件输出流对象*/\x0d\x0aFile file = null\x0d\x0aFileOutputStream fos = null\x0d\x0a \x0d\x0a/**定义输入流,使用socket的inputStream对数据包进行输入*/\x0d\x0aInputStream is = null\x0d\x0a \x0d\x0a/**定义byte数组来作为数据包的存储数据包*/\x0d\x0abyte[] buffer = new byte[4096 * 5]\x0d\x0a \x0d\x0a/**用来接收文件发送请求的字符串*/\x0d\x0aString comm = null\x0d\x0a\x0d\x0a/**建立socekt通信,等待服务器进行连接*/\x0d\x0atry {\x0d\x0ass = new ServerSocket(4004)\x0d\x0as = ss.accept()\x0d\x0a} catch (IOException e) {\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a\x0d\x0a/**读取一行客户端发送过来的约定信息*/\x0d\x0atry {\x0d\x0aInputStreamReader isr = new InputStreamReader(s.getInputStream())\x0d\x0aBufferedReader br = new BufferedReader(isr)\x0d\x0acomm = br.readLine()\x0d\x0a} catch (IOException e) {\x0d\x0aSystem.out.println("服务器与客户端断开连接")\x0d\x0a}\x0d\x0a \x0d\x0a/**开始解析客户端发送过来的请求命令*/\x0d\x0aint index = comm.indexOf("/#")\x0d\x0a \x0d\x0a/**判断协议是否为发送文件的协议*/\x0d\x0aString xieyi = comm.substring(0, index)\x0d\x0aif(!xieyi.equals("111")){\x0d\x0aSystem.out.println("服务器收到的协议码不正确")\x0d\x0areturn\x0d\x0a}\x0d\x0a \x0d\x0a/**解析出文件的名字和大小*/\x0d\x0acomm = comm.substring(index + 2)\x0d\x0aindex = comm.indexOf("/#")\x0d\x0aString filename = comm.substring(0, index).trim()\x0d\x0aString filesize = comm.substring(index + 2).trim()\x0d\x0a\x0d\x0a/**创建空文件,用来进行接收文件*/\x0d\x0afile = new File(filename)\x0d\x0aif(!file.exists()){\x0d\x0atry {\x0d\x0afile.createNewFile()\x0d\x0a} catch (IOException e) {\x0d\x0aSystem.out.println("服务器端创建文件失败")\x0d\x0a}\x0d\x0a}else{\x0d\x0a/**在此也可以询问是否覆盖*/\x0d\x0aSystem.out.println("本路径已存在相同文件,进行覆盖")\x0d\x0a}\x0d\x0a \x0d\x0a/**【以上就是客户端代码中写到的服务器的准备部分】*/\x0d\x0a\x0d\x0a/**\x0d\x0a * 服务器接收文件的关键代码*/\x0d\x0atry {\x0d\x0a/**将文件包装到文件输出流对象中*/\x0d\x0afos = new FileOutputStream(file)\x0d\x0along file_size = Long.parseLong(filesize)\x0d\x0ais = s.getInputStream()\x0d\x0a/**size为每次接收数据包的长度*/\x0d\x0aint size = 0\x0d\x0a/**count用来记录已接收到文件的长度*/\x0d\x0along count = 0\x0d\x0a \x0d\x0a/**使用while循环接收数据包*/\x0d\x0awhile(count 回答于 2022-12-11

Java代码实现文件上传

FormFile file=manform.getFile() 

  String newfileName = null

  String newpathname=null

  String fileAddre="/numUp"

  try {

   InputStream stream = file.getInputStream()// 把文件读入

    String filePath = request.getRealPath(fileAddre)//取系统当前路径

          File file1 = new File(filePath)//添加了自动创建目录的功能

       ((File) file1).mkdir()   

    newfileName = System.currentTimeMillis()

     + file.getFileName().substring(

       file.getFileName().lastIndexOf('.'))

   ByteArrayOutputStream baos = new ByteArrayOutputStream()

   OutputStream bos = new FileOutputStream(filePath + "/"

     + newfileName)

   newpathname=filePath+"/"+newfileName

   System.out.println(newpathname)

   // 建立一个上传文件的输出流

    System.out.println(filePath+"/"+file.getFileName())

   int bytesRead = 0

   byte[] buffer = new byte[8192]

   while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {

    bos.write(buffer, 0, bytesRead)// 将文件写入服务器

   }

   bos.close()

   stream.close()

    } catch (FileNotFoundException e) {

   e.printStackTrace()

  } catch (IOException e) {

   e.printStackTrace()

  }