android 上传大文件中断

maikkk 发布于 2012/06/21 17:26
阅读 5K+
收藏 1
/**
* @param uploadUrl 上传处理的页面
* @param srcPath 图片文件路径
*/
public static  void UpLoadImage(String uploadUrl, String srcPath) 
{
String end = "\r\n";
String twoHyphens = "--";
String boundary = "------WebKitFormBoundaryA9mFw94fYlrTjynQ";
try 
{

URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
//httpURLConnection.setChunkedStreamingMode(51200);// 128K
// 允许输入输出流
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
// 使用POST方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "keep-alive");
httpURLConnection.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
httpURLConnection.setRequestProperty("Accept:","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpURLConnection.setRequestProperty("Accept-Encoding","gzip,deflate,sdch");
httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8");
httpURLConnection.setRequestProperty("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3");
httpURLConnection.setChunkedStreamingMode(51200);
httpURLConnection.setConnectTimeout(10 * 60 * 1000);
String beginbound = boundary + end
+ "Content-Disposition: form-data; name=\"file\"; filename=\"" + srcPath.substring(srcPath.lastIndexOf("/") + 1) + "\"" + end 
+ "Content-Type: image/jpeg" + end
+ end;
String endbound = end + boundary + end
+ "Content-Disposition: form-data; name=\"submit\"" + end + end
+ "Submit" + end 
+ boundary + "--" + end;
FileInputStream fis = new FileInputStream(srcPath);
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(fis.available() + beginbound.length() + endbound.length()));
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
dos.writeBytes(beginbound);
// dos.writeBytes(twoHyphens + boundary + end);
// dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
// + srcPath.substring(srcPath.lastIndexOf("/") + 1)
// + "\""
// + end);
// dos.writeBytes(end);

byte[] buffer = new byte[1024]; // 8k
int count = 0;
int readcount = 0;
int totalcount = fis.available();
// 读取文件
while ((count = fis.read(buffer)) != -1) {
Log.i("UpLoadImage", "write count:" + readcount + ",total:" + totalcount );
dos.write(buffer, 0, count);
Log.i("UpLoadImage", "write count ok" );
readcount+=count;
}
Log.i("UpLoadImage", "fis.close" );
fis.close();

//dos.writeBytes(end);
//dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.writeBytes(endbound);
dos.flush();
Log.i("UpLoadImage", "flush ok" );
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = "";
String t = br.readLine();
while(t != null)
{
result += t;
t = br.readLine();
}
Log.i("UpLoadImage", result );
// Toast.makeText(this, result, Toast.LENGTH_LONG).show();
dos.close();
is.close();

catch (Exception e) 
{
e.printStackTrace();
Log.i("UpLoadImage", e.getMessage());
// setTitle(e.getMessage());
}
}

上面代码是模仿浏览器上传文件

借鉴PHP上传文件 http://www.w3school.com.cn/php/php_file_upload.asp

但是上传超过2m文件时候大概传送到90%就中断了.

如果在浏览器上传就没事

加载中
0
maikkk
maikkk
原来是协议格式错了
d
dddddevil
您说协议格式错了,那把正确的协议格式发出来让大家参考下啊。。感激不尽
刘学炜
刘学炜
顶~
0
大帥zzZZ℡

请教一下:

您说协议格式错了,是不是要改成这样

httpURLConnection.setRequestProperty("Content-Type","application/octet-stream=" + boundary);

0
大帥zzZZ℡

能给一份完整的代码吗?就是关于这个的 谢谢

 

大帥zzZZ℡
回复 @maikkk : 谢谢啦珍藏
maikkk
maikkk
已经放出来了
0
maikkk
maikkk
	public static  void UpLoadImage(String uploadUrl, String srcPath,NetUploadPictureListen listen) 
	{
		String end = "\r\n";
		String twoHyphens = "--";
		String boundary = "------WebKitFormBoundaryA9mFw94fYlrTjynQ";
		try 
		{
			File file = new File(srcPath);  

			URL url = new URL(uploadUrl);
			HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
			// 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
			// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
			//httpURLConnection.setChunkedStreamingMode(51200);// 128K
			// 允许输入输出流
			httpURLConnection.setDoInput(true);
			httpURLConnection.setDoOutput(true);
			httpURLConnection.setUseCaches(false);
			// 使用POST方法
			httpURLConnection.setRequestMethod("POST");
			httpURLConnection.setRequestProperty("Connection", "keep-alive");
			httpURLConnection.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
			httpURLConnection.setRequestProperty("Accept:","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			httpURLConnection.setRequestProperty("Accept-Encoding","gzip,deflate,sdch");
			httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8");
			httpURLConnection.setRequestProperty("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3");
			httpURLConnection.setRequestProperty("Referer","http://你的域名/uploadfile.html");
			httpURLConnection.setRequestProperty("HOST", "你的域名");  
			httpURLConnection.setConnectTimeout(10 * 60 * 1000);
			String beginbound = "--" + boundary + end
				+ "Content-Disposition: form-data; name=\"file\"; filename=\"" + srcPath.substring(srcPath.lastIndexOf("/") + 1) + "\"" + end 
				+ "Content-Type: image/jpeg" + end
				+ end;
				
			
			String endbound =  end + "--" + boundary + end
				+ "Content-Disposition: form-data; name=\"submit\"" + end + end
				+ "Submit" + end 
				+ "--" + boundary + "--" + end;
			
			
			
			httpURLConnection.setRequestProperty("Content-Length", String.valueOf(file.length() + beginbound.length() + endbound.length()));


			DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
			out.writeBytes(beginbound);
			FileInputStream in = new FileInputStream(srcPath);
			byte[] buffer = new byte[1024]; // 8k
			int count = 0;
			int readcount = 0;
			int totalcount = in.available();
			// 读取文件
			while ((count = in.read(buffer)) != -1) {
				Log.i("UpLoadImage", "write count:" + readcount + ",total:" + totalcount );
				
				out.write(buffer, 0, count);
				out.flush();
				Log.i("UpLoadImage", "write count ok" );
				readcount+=count;
			}
			Log.i("UpLoadImage", "fis.close" );
			

			out.writeBytes(endbound);
			
			
			//dos.flush();
			Log.i("UpLoadImage", "flush ok" );
			InputStream is = httpURLConnection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			BufferedReader br = new BufferedReader(isr);
			String result = "";
			String t = br.readLine();
			while(t != null)
			{
				result += t;
				t = br.readLine();
			}
			
			
			
			Log.i("UpLoadImage", result );
			// Toast.makeText(this, result, Toast.LENGTH_LONG).show();
			listen.OnResult(result);
			is.close();
			in.close();
			out.close();

		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			Log.i("UpLoadImage", e.getMessage());
			// setTitle(e.getMessage());
		}
	}

0
w
wchhuangya
这个方法是有问题的~和手机是有关系的~如果手机可用内存不够~会导致程序崩溃……
0
ceachother
ceachother
修改后 我上传还是不能超过20M吗?
0
maikkk
maikkk
没试过传20M那么大的哦,我上面修改的是协议格式错误
0
C
Conan_Zhi
我也是这种方式上传文件   我的协议是和后台的保持一致的 没有问题  上传图片 小文件没有问题  但是上传大文件  上传3M到4M 就异常退出了  请问这是什么原因  是不是和手机的内存大小有关 呢    还请指教   感激不尽!
返回顶部
顶部