2
A
回答
3
前段时间有同样的问题。这是一个代码。
public static int upLoad2Server(String sourceFileUri)
{
String upLoadServerUri = "your remote server link";
// String [] string = sourceFileUri;
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String responseFromServer = "";
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
Log.e("Huzza", "Source File Does not exist");
return 0;
}
try { // open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of maximum size
Log.i("Huzza", "Initial .available : " + bytesAvailable);
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("Upload file to server", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
// close streams
Log.i("Upload file to server", fileName + " File is written");
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
e.printStackTrace();
}
//this block will give the response of upload link
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
Log.i("Huzza", "RES Message: " + line);
}
rd.close();
} catch (IOException ioex) {
Log.e("Huzza", "error: " + ioex.getMessage(), ioex);
}
return serverResponseCode; // like 200 (Ok)
} // end upLoad2Server
相关问题
- 1. Android youtube视频上传?
- 2. Android视频上传到Youtube
- 3. Youtube上传视频Android
- 4. 在Android的特定频道上传YouTube视频的视频
- 5. 在Summernote中上传视频
- 6. 在Android上传视频到Vimeo
- 7. 视频上传到Facebook在Android的
- 8. 在上传视频时观看视频
- 9. Android上的Twitter视频上传失败
- 10. YouTube上的视频上传 - Android
- 11. 上传视频
- 12. 上传视频
- 13. 安卓:在上传视频
- 14. 视频上传在PHP
- 15. 在黑莓上传视频
- 16. 代码上传视频从Android到PHP
- 17. 从android应用上传视频到youtube
- 18. Android使用gdata向YouTube上传视频?
- 19. Appcelerator:如何使用android上传视频?
- 20. Android视频上传到Php服务器?
- 21. 将视频从android上传到youtube?
- 22. 从android设备上传视频并在服务器上发布该视频
- 23. PHP视频上传
- 24. PHP上传视频
- 25. Grandnode上传视频
- 26. FLV视频上传?
- 27. 在Android中的背景中上传视频
- 28. 在Android上预载视频
- 29. 在Android上的Youtube视频
- 30. 在php中压缩视频并上传
您能详细解释一下您的问题吗?您正试图将手机存储中的视频上传到服务器上,对吗?是否需要以编程方式执行此操作?你有没有考虑发布[android.stackexchange](http://android.stackexchange.com/)? –