首页 > 编程知识 正文

java文件传输,java文件传输代码

时间:2023-12-29 13:16:44 阅读:330057 作者:OPCP

本文目录一览:

java上传文件到坚果云

1、首先先打开电脑,点击桌面上的java软件。

2、其次进入之后,找到想要上传的文件,点击左上方的三个点,找到上传方式。

3、最后在选择上传的位置坚果云,点击上传即可。

java如何实现文件上传

public static int transFile(InputStream in, OutputStream out, int fileSize) {

int receiveLen = 0;

final int bufSize = 1000;

try {

byte[] buf = new byte[bufSize];

int len = 0;

while(fileSize - receiveLen bufSize)

{

len = in.read(buf);

out.write(buf, 0, len);

out.flush();

receiveLen += len;

System.out.println(len);

}

while(receiveLen fileSize)

{

len = in.read(buf, 0, fileSize - receiveLen);

System.out.println(len);

out.write(buf, 0, len);

receiveLen += len;

out.flush();

}

} catch (IOException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

return receiveLen;

}

这个方法从InputStream中读取内容,写到OutputStream中。

那么发送文件方,InputStream就是FileInputStream,OutputStream就是Socket.getOutputStream.

接受文件方,InputStream就是Socket.getInputStream,OutputStream就是FileOutputStream。

就OK了。 至于存到数据库里嘛,Oracle里用Blob。搜索一下,也是一样的。从Blob能获取一个输出流。

java中怎样上传文件

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();

  }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。