java上传文件内存溢出_java - 使用outputstream上传时文件已损坏 - 堆栈内存溢出

每次我上传带有部分数据的excel时,上传的文件都会损坏,并且我无法在aspose工作簿中使用此上传的文件(在某些情况下它也可以工作,我不知道为什么)。 甚至在我打开的同时,excel也会显示损坏的文件消息。 这是片段

InputStream stream = file.getInputStream();

OutputStream os = new FileOutputStream("path_to_xlsFile.xls");

BufferedOutputStream bos = new BufferedOutputStream(os);

int bytesRead;

byte[] buffer = new byte[8192];

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

bos.write(buffer, 0, bytesRead);

}

bos.flush();

bos.close();

os.close();

stream.close();

通过aspose库打开工作簿时,对于相同的上传文件,出现的错误是

java.lang.IllegalStateException: The FAT in the structured storage document seems to be corrupted.

at com.aspose.cells.py.b(Unknown Source)

at com.aspose.cells.py.b(Unknown Source)

at com.aspose.cells.py.(Unknown Source)

at com.aspose.cells.Workbook.a(Unknown Source)

at com.aspose.cells.Workbook.open(Unknown Source)

要么

java.lang.IllegalStateException: The structured storage seems to be corrupt.

at com.aspose.cells.mm.(Unknown Source)

at com.aspose.cells.py.c(Unknown Source)

at com.aspose.cells.py.b(Unknown Source)

at com.aspose.cells.py.(Unknown Source)

at com.aspose.cells.Workbook.a(Unknown Source)

at com.aspose.cells.Workbook.open(Unknown Source)

要么

java.lang.IllegalArgumentException: length

at com.aspose.cells.b.a.d.h.b(Unknown Source)

at com.aspose.cells.py.c(Unknown Source)

at com.aspose.cells.py.b(Unknown Source)

at com.aspose.cells.py.(Unknown Source)

at com.aspose.cells.Workbook.a(Unknown Source)

at com.aspose.cells.Workbook.open(Unknown Source)

编辑

如果我使用file.transferTo(outputFile),那么对于少数文件,我也会遇到此异常

java.lang.IllegalStateException: The FAT in the structured storage document seems to be corrupted.

at com.aspose.cells.py.b(Unknown Source)

at com.aspose.cells.py.b(Unknown Source)

at com.aspose.cells.py.(Unknown Source)

at com.aspose.cells.Workbook.a(Unknown Source)

at com.aspose.cells.Workbook.open(Unknown Source)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java使用HttpURLConnection上文件可以通过以下步骤实现: 1. 创建URL对象,指定上文件的URL地址。 2. 打开HTTP连接,并设置请求方法为POST。 3. 设置HTTP请求头部信息,包括Content-Type和boundary。 4. 设置HTTP请求体信息,包括上文件的内容和分割线。 5. 读取服务器端返回的响应信息。 示例代码如下: ```java import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class FileUploader { public static void main(String[] args) throws Exception { String urlStr = "http://example.com/upload"; String filePath = "C:/test.txt"; URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); String boundary = "---------------------------" + System.currentTimeMillis(); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream out = conn.getOutputStream(); // 写入分割线 out.write(("--" + boundary + "\r\n").getBytes()); // 写入文件名 out.write(("Content-Disposition: form-data; name=\"file\"; filename=\"" + filePath + "\"\r\n").getBytes()); // 写入文件类型 out.write(("Content-Type: application/octet-stream\r\n").getBytes()); // 写入空行 out.write(("\r\n").getBytes()); // 写入文件内容 FileInputStream fileIn = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int len; while ((len = fileIn.read(buffer)) != -1) { out.write(buffer, 0, len); } fileIn.close(); // 写入空行 out.write(("\r\n").getBytes()); // 写入分割线 out.write(("--" + boundary + "--\r\n").getBytes()); out.flush(); out.close(); // 读取响应内容 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } ``` 其中,filePath为需要上文件路径,urlStr为服务器端接收文件的URL地址。在示例代码中,我们使用了multipart/form-data类型的请求体格式,可以在请求头部信息中设置Content-Disposition、Content-Type等参数。同,我们也设置了一个分割线boundary,用于分隔不同的请求体部分。最后,我们通过读取服务器端返回的响应信息,来获取上文件的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值