解析一下while((len=fis.read(data))!=-1){ out.write(data,0,len); }
时间: 2023-05-18 13:05:24 浏览: 156
这段代码是一个循环,其含义是从输入流fis中读取数据到缓冲区data中,每次读取的数据长度为len,然后将缓冲区中的数据写入输出流out中,直到读取到文件末尾(即len为-1)为止。
具体解析如下:
1. while循环:这是一个while循环,其条件是(len=fis.read(data))!=-1,即每次从输入流fis中读取数据到缓冲区data中,并将读取的数据长度赋值给len,如果读取到文件末尾,则len为-1,循环结束。
2. fis.read(data):从输入流fis中读取数据到缓冲区data中,返回值为读取的数据长度,如果读取到文件末尾,则返回-1。
3. out.write(data,0,len):将缓冲区data中的数据写入输出流out中,写入的数据长度为len,即每次写入读取到的数据长度,从缓冲区data的第0个位置开始写入。
4. 总结:这段代码的作用是将输入流fis中的数据读取到缓冲区data中,然后将缓冲区中的数据写入输出流out中,直到读取到文件末尾。
相关问题
编写程序,在“E:\5e\”路径下创建文件 out.txt,并使用缓冲流和文件流,将 “E:\data.txt 文件中的内容复制到 out.txt。
好的,下面是使用缓冲流和文件流将 `E:\data.txt` 文件中的内容复制到 `E:\5e\out.txt` 文件中的 Java 代码:
```java
import java.io.*;
public class CopyFileDemo {
public static void main(String[] args) {
try {
// 创建输入流和输出流
FileInputStream fis = new FileInputStream("E:\\data.txt");
FileOutputStream fos = new FileOutputStream("E:\\5e\\out.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 将数据从输入流复制到输出流
byte[] buf = new byte[1024];
int len = 0;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关闭输入流和输出流
bis.close();
bos.close();
System.out.println("复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
解释一下这段代码:
1. 首先,我们创建一个 `FileInputStream` 对象,它表示要读取哪个文件。这里我们假设要读取的文件名为 `data.txt`,并且它位于 E 盘根目录下。
2. 然后,我们创建一个 `FileOutputStream` 对象,它表示要向哪个文件写入内容。这里我们假设要写入的文件名为 `out.txt`,并且它位于 E 盘下的 `5e` 文件夹中。
3. 接着,我们将输入流和输出流分别包装成 `BufferedInputStream` 和 `BufferedOutputStream`,以便进行缓冲读写,提高效率。
4. 然后,我们进入一个循环,反复从输入流中读取数据,然后写入到输出流中,直到读取到文件末尾。
5. 最后,我们关闭输入流和输出流,并输出提示信息表示复制成功。如果在复制过程中发生异常,我们会捕获并输出异常信息。
希望这个代码对您有所帮助。如果您有任何问题,可以随时问我。
文件服务的文件分片上传接口说明: 访问路径接口:/0bjectFile/UploadMultipart Ip:192168.8.7 Port:4035 请求方式:POST请求参数: 参数 参数名称 备注 是否必填 file 分片文件 字节数组 是 bucketName 桶名 String 是 taskid 任务id 第一分片进入不需要传第二分片后必填,String size 分片文件大小 同一任务文件,分片大小一样.int 是 chunk 当前第几分片 int 是 chunkTotal 分片总数 int 是 filePath 文件保存路径 String 是 请求示例:请求示例 192.168.8.7:4035/0bjectFile/UploadMultipart //form-data提交格式 file:[1. bucketName:“1” taskid"13245-131654-a50 size:1024, chunk 1 chunkTotal:3 filePath:"/test/1 jpg如何用java代码实现
可以使用Java的HttpURLConnection类发送POST请求实现文件分片上传。具体代码实现如下:
```
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileUpload {
public static void main(String[] args) throws Exception {
String url = "http://192.168.8.7:4035/0bjectFile/UploadMultipart"; // 请求地址
String filePath = "D:\\test\\1.jpg"; // 文件路径
String bucketName = "1"; // 桶名
int chunk = 1; // 当前分片
int chunkTotal = 3; // 分片总数
int size = 1024; // 分片大小
String taskId = "13245-131654-a50"; // 任务id
String savePath = "/test/1.jpg"; // 文件保存路径
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[size];
int len = -1;
while ((len = fis.read(buffer)) != -1) {
String result = upload(url, buffer, bucketName, taskId, size, chunk, chunkTotal, savePath);
// 解析返回结果...
}
fis.close();
}
private static String upload(String url, byte[] buffer, String bucketName, String taskId, int size, int chunk, int chunkTotal, String savePath) throws Exception {
HttpURLConnection conn = null;
OutputStream out = null;
InputStream in = null;
ByteArrayOutputStream baos = null;
try {
URL urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
out = conn.getOutputStream();
// 文件分片参数
StringBuilder sb = new StringBuilder();
sb.append("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + taskId + "\"\r\n");
sb.append("Content-Type: application/octet-stream\r\n\r\n");
out.write(sb.toString().getBytes());
out.write(buffer);
sb = new StringBuilder();
sb.append("\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
sb.append("Content-Disposition: form-data; name=\"bucketName\"\r\n\r\n");
sb.append(bucketName + "\r\n");
sb.append("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
sb.append("Content-Disposition: form-data; name=\"taskId\"\r\n\r\n");
sb.append(taskId + "\r\n");
sb.append("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
sb.append("Content-Disposition: form-data; name=\"size\"\r\n\r\n");
sb.append(size + "\r\n");
sb.append("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
sb.append("Content-Disposition: form-data; name=\"chunk\"\r\n\r\n");
sb.append(chunk + "\r\n");
sb.append("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
sb.append("Content-Disposition: form-data; name=\"chunkTotal\"\r\n\r\n");
sb.append(chunkTotal + "\r\n");
sb.append("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
sb.append("Content-Disposition: form-data; name=\"filePath\"\r\n\r\n");
sb.append(savePath + "\r\n");
sb.append("------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n");
out.write(sb.toString().getBytes());
out.flush();
in = conn.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = -1;
while ((len = in.read(buf)) != -1) {
baos.write(buf, 0, len);
}
return new String(baos.toByteArray());
} finally {
if (baos != null) {
baos.close();
}
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (conn != null) {
conn.disconnect();
}
}
}
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)