你尝试过使用过吗?

con.setChunkedStreamingMode(1024);

这将帮助您将数据块化为特定大小,这样您就不必将整个文件保留在内存中。

更新:

尝试使用以下方法。我使用此方法上传一个没有Exception的80 MB文件。

public String sendFileToServer(String filename, String targetUrl) {

String response = "error";

Log.e("Image filename", filename);

Log.e("url", targetUrl);

HttpURLConnection connection = null;

DataOutputStream outputStream = null;

// DataInputStream inputStream = null;

String pathToOurFile = filename;

String urlServer = targetUrl;

String lineEnd = "\r\n";

String twoHyphens = "--";

String boundary = "*****";

DateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH:mm:ss");

int bytesRead, bytesAvailable, bufferSize;

byte[] buffer;

int maxBufferSize = 1 * 1024;

try {

FileInputStream fileInputStream = new FileInputStream(new File(

pathToOurFile));

URL url = new URL(urlServer);

connection = (HttpURLConnection) url.openConnection();

// Allow Inputs & Outputs

connection.setDoInput(true);

connection.setDoOutput(true);

connection.setUseCaches(false);

connection.setChunkedStreamingMode(1024);

// Enable POST method

connection.setRequestMethod("POST");

connection.setRequestProperty("Connection", "Keep-Alive");

connection.setRequestProperty("Content-Type",

"multipart/form-data;boundary=" + boundary);

outputStream = new DataOutputStream(connection.getOutputStream());

outputStream.writeBytes(twoHyphens + boundary + lineEnd);

String connstr = null;

connstr = "Content-Disposition: form-data; name=\"uploadedfile\";filename=\""

+ pathToOurFile + "\"" + lineEnd;

Log.i("Connstr", connstr);

outputStream.writeBytes(connstr);

outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);

buffer = new byte[bufferSize];

// Read file

bytesRead = fileInputStream.read(buffer, 0, bufferSize);

Log.e("Image length", bytesAvailable + "");

try {

while (bytesRead > 0) {

try {

outputStream.write(buffer, 0, bufferSize);

} catch (OutOfMemoryError e) {

e.printStackTrace();

response = "outofmemoryerror";

return response;

}

bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);

bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}

} catch (Exception e) {

e.printStackTrace();

response = "error";

return response;

}

outputStream.writeBytes(lineEnd);

outputStream.writeBytes(twoHyphens + boundary + twoHyphens

+ lineEnd);

// Responses from the server (code and message)

int serverResponseCode = connection.getResponseCode();

String serverResponseMessage = connection.getResponseMessage();

Log.i("Server Response Code ", "" + serverResponseCode);

Log.i("Server Response Message", serverResponseMessage);

if (serverResponseCode == 200) {

response = "true";

}

String CDate = null;

Date serverTime = new Date(connection.getDate());

try {

CDate = df.format(serverTime);

} catch (Exception e) {

e.printStackTrace();

Log.e("Date Exception", e.getMessage() + " Parse Exception");

}

Log.i("Server Response Time", CDate + "");

filename = CDate

+ filename.substring(filename.lastIndexOf("."),

filename.length());

Log.i("File Name in Server : ", filename);

fileInputStream.close();

outputStream.flush();

outputStream.close();

outputStream = null;

} catch (Exception ex) {

// Exception handling

response = "error";

Log.e("Send file Exception", ex.getMessage() + "");

ex.printStackTrace();

}

return response;

}

android多文件上传错误,在Android中上传大文件而不会出现内存错误相关推荐

  1. 4种大文件传输工具和软件,用于共享大文件

    无论是个人还是与团队一起工作,大文件传输软件和网站都能协助提高工作效率.有效地管理工作内容.疫情原因有时我们不得不居家办公,在这种情况下可以分享文件的工具就显得尤为重要. 每个公司都需要一个文件传输软 ...

  2. linux文件读写 c语言,Linux 下 C语言大文件读写(大于4G)

    以下的做法整理自论坛上的帖子. 如何create大文件 要大就非常大,1T吧. 有两种方法: 一.dd dd if=/dev/zero of=1T.img bs=1G seek=1024 count= ...

  3. pandas使用to_feather函数将dataframe保存为feather文件(需要依赖pyarrow包)提升大文件读取效率、pandas使用read_feather函数读取feather文件

    pandas使用to_feather函数将dataframe保存为feather文件(需要依赖pyarrow包)提升大文件读取效率.pandas使用read_feather函数读取feather文件. ...

  4. ffmpeg android mac,Mac上交叉编译ffmpeg For Android

    介绍通过mac os系统编译给Android平台上可以使用的ffmpeg静态库. 编译ffmpeg的时候,需要在命令行上,配置编译的路径以及设置不同的属性值,如果一个个命令行输入的话,很容易出错,出错 ...

  5. kodi android 卡顿,给Kodi设置缓存来解决播放大文件卡顿

    给Kodi设置缓存来解决播放大文件卡顿 2021-03-02 16:21:20 28点赞 355收藏 31评论 Kodi可以算是电视盒子上最棒的播放器了,它具有强大的功能,优美的界面,相信很多人也都是 ...

  6. c#如何上传大文件到服务器,asp.net(C#)中上传大文件的几中常见应用方法

    asp.net(C#)中上传大文件的几中常见应用方法 更新时间:2008年11月25日 10:05:46   作者: 最近博客需要做一个文件上下载功能,我从网上找了点资料,整理了下希望对大家有帮助! ...

  7. MOSS2007 无法上传超过30M或者50M的大文件解决办法 (转)

    转自:http://www.cnblogs.com/codingart/articles/1718032.html 1 如果MOSS2007 无法上传超过30M的文件 解决办法:可能是iis7上传大文 ...

  8. vue 文件及描述信息一起上传_用Vue实现一个大文件上传和断点续传

    前言 这段时间面试官都挺忙的,频频出现在博客文章标题,虽然我不是特别想蹭热度,但是实在想不到好的标题了-.-,蹭蹭就蹭蹭 :) 事实上我在面试的时候确实被问到了这个问题,而且是一道在线 coding ...

  9. h5如何上传文件二进制流_Hadoop如何将TB级大文件的上传性能优化上百倍?

    这篇文章,我们来看看,Hadoop的HDFS分布式文件系统的文件上传的性能优化. 首先,我们还是通过一张图来回顾一下文件上传的大概的原理. 由上图所示,文件上传的原理,其实说出来也简单. 比如有个TB ...

最新文章

  1. C++虚析构和纯虚析构
  2. Luogu P1550 [USACO08OCT]打井Watering Hole
  3. 开滦二中2021高考成绩查询,2021唐山中考录取分数线查询
  4. 十面阿里Java程序员终拿下阿里P6offer
  5. Python的安装部署
  6. IntelliJ Idea学习笔记004---IDEA中maven没有了
  7. 跨域iframe高度自适应(兼容IE/FF/OP/Chrome)
  8. php 创建 cookie文件,php创建、获取cookie及基础要点分析
  9. 在 mysql数据库怎么知道的ip_用户名_密码_数据库_数据库ip怎么查
  10. 录制计算机课,ClassIn怎么录制视频 录课操作步骤
  11. 【网页期末作业】用HTML+CSS做一个漂亮简单的学校官网
  12. Flash actionscrip视频教程
  13. mysql 主从 只读,mysql主从复制(从库只读)
  14. ufldl tutorial excise - Supervised Learning and Optimization
  15. 【入门2】分支结构 P1422 小玉家的电费
  16. Source Insight 使用
  17. JNI HelloWorld 亲测可以。
  18. 快手双核心时代宣告结束,互联网“退休潮”还在继续?
  19. ABAP EWM HU打包
  20. 【转】利用tmail.exe 命令参数来发送邮件

热门文章

  1. Bailian2799 浮点数格式【文本处理+最值】
  2. Java 系统工具类 —— System、Scanner、Runtime
  3. matlab 矢量化编程(四)—— 标量函数转化为能够处理矢量的函数
  4. 对数的应用 —— 数位(digits)的个数
  5. web 开发 —— html 与 css(div)
  6. [面试] 算法(八)—— 树
  7. Trick(三)——循环左移的实现
  8. 机器学习算法工程师的经典面试问题
  9. 华为抓取错误日志在哪里_分析Spider抓取情况和SEO优化
  10. java单例模式_Java 实现单例模式的 9 种方法