android http客户端下载广告上传文件
堆垛机。。。 我有一个共享文件夹的Web服务器。。。它的地址是:android http客户端下载广告上传文件,android,android-ndk,android-networking,Android,Android Ndk,Android Networking,堆垛机。。。 我有一个共享文件夹的Web服务器。。。它的地址是: "http://192.168.1.1:9999/folder/0" 我在谷歌上搜索了很多关于下载文件的信息,需要帮助 下载就是成功。。。如何将文件上传到droid???(服务器上的权限已启用) 网络连接经常失败,尤其是对于移动客户端。例如,如果您从Wifi切换到3G,则现有网络连接将中断,您需要重试该请求。Apache HttpClient注册了默认的DefaultHttpRequestRetryHandler对象
"http://192.168.1.1:9999/folder/0"
我在谷歌上搜索了很多关于下载文件的信息,需要帮助
要上载文件,可以使用多部分请求:
要上载文件,可以使用多部分请求:
SoapRequestProcessor.authKey()-是什么?你可以跳过那一行。实际上,在我的例子中还需要身份验证密钥。SoapRequestProcessor.authKey()-是什么?你可以跳过这一行。实际上,在我的例子中,身份验证密钥也是必需的。
case R.id.download:
try {
java.net.URL u = new java.net.URL("http://192.168.1.1:9999/file/2/01.mp3");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File("/sdcard/in", "01.mp3"));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 )
{
f.write(buffer,0, len1);
}
f.close();
}
catch (Exception e) {
System.out.println("Nay, did not work");
textView.setText(e.getMessage());
}
break;
Please make sure you have the required Web Services for Posting.
Just pass the file and url of server and the following should run.
try{
int maxBufferSize=1024*1024;
File file = new File(YourPathToFile);
String fileName = file.getName();
URL url = new URL(YourUrlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("GET");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
connection.setChunkedStreamingMode(maxBufferSize);
outputStream = new DataOutputStream( connection.getOutputStream() );
fileInputStream = new FileInputStream(file);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"strAuthKey\"" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(SoapRequestProcessor.authKey());//authentication key
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"mediaName\"" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(fileName); //file.lastModified()+"_"+
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"IMAGEFILE\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
outputStream.close();
Log.d("RESPONSE","--"+connection.getResponseMessage());
}
catch (Exception e) {
Log.i("Exception: ",e.toString());
// TODO: handle exception
}
client = new DefaultHttpClient(...);
HttpPost post = new HttpPost("some url");
MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipart.addPart("Description", new StringBody("Description"));
multipart.addPart("File", new FileBody(fileObject));
post.setEntity(multipart);
client.execute(post, new ResponseHandler() {
@Override
public Object handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
//handle response
}
});