java中的文件io,如何在Java中模拟文件IO?

How can I create a class MockFile mimicking java.io.File w.r.t. file read and write? I using everywhere my own methods instead of new FileInputStream(....) and new FileOutputStream(....), so this part is no problem (I always delegate to the appropriate stream). The non-trivila part is the implementation of my MockFileInputStream and MockFileOutputStream in more complicated cases.

There's is no problem, when I first write to a file and then read it, I can simply use a ByteArrayOutputStream and so on. That's simple, but with interleaving reads and writes it can't work. Any better idea than writing my own version of ByteArrayOutputStream?

解决方案

I would use a real file and a real FileInputStream and FileOutputStream. Otherwise you're just exercising test code: pretty pointless really.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的HttpURLConnection类来模拟表单上传文件。以下是一个简单的示例代码: ```java import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class FileUploader { public static void main(String[] args) throws IOException { String url = "http://example.com/upload"; // 替换为实际的上传地址 String filePath = "/path/to/file"; // 替换为实际的文件路径 URL uploadUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) uploadUrl.openConnection(); // 设置请求方法为POST connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"); // 设置请求参数 File file = new File(filePath); String fileName = file.getName(); String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; String lineBreak = "\r\n"; String postData = "--" + boundary + lineBreak + "Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"" + lineBreak + "Content-Type: application/octet-stream" + lineBreak + lineBreak; // 发送请求 OutputStream outputStream = connection.getOutputStream(); outputStream.write(postData.getBytes()); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fileInputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } String endData = lineBreak + "--" + boundary + "--" + lineBreak; outputStream.write(endData.getBytes()); // 获取响应 int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // 上传成功 } else { // 上传失败 } // 关闭连接 outputStream.close(); connection.disconnect(); } } ``` 在上面的代码,我们首先创建一个URL对象,然后打开一个HttpURLConnection连接。我们设置请求方法为POST,同时将其设置为允许输出。我们还设置了请求头的Content-Type字段,以指示我们正在发送一个multipart/form-data类型的表单请求。 接着,我们设置请求参数。我们将要上传的文件读入一个byte数组,并将其作为请求主体发送。我们还设置了一个分隔符,以便服务器能够识别请求的不同部分。 最后,我们发送请求,并读取响应。如果响应代码为HTTP_OK,表示上传成功。如果上传失败,我们可以根据响应代码采取不同的措施。最后,我们关闭连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值