《Android上传文件到服务器.docx》由会员分享,可在线阅读,更多相关《Android上传文件到服务器.docx(6页珍藏版)》请在三一办公上搜索。
1、Android上传文件到服务器 Android上传文件到服务器,通常采用构造http协议的方法,模拟网页POST方法传输文件,服务器端可以采用JavaServlet或者PHP来接收要传输的文件。使用JavaServlet来接收文件的方法比较常见,在这里给大家介绍一个简单的服务器端使用PHP语言来接收文件的例子。 服务器端代码比较简单,接收传输过来的文件: php view plain copy print? 1. 手机客户端代码: java view plain copy print? 1. package com.figo.uploadfile; 2. 3. import java.io.B
2、ufferedReader; 4. import java.io.DataOutputStream; 5. import java.io.FileInputStream; 6. import java.io.InputStream; 7. import java.io.InputStreamReader; 8. import .HttpURLConnection; 9. import .URL; 10. import android.app.Activity; 11. import android.os.Bundle; 12. import android.view.View; 13. imp
3、ort android.widget.Button; 14. import android.widget.TextView; 15. import android.widget.Toast; 16. 17. public class UploadfileActivity extends Activity 18. 19. / 要上传的文件路径,理论上可以传输任何文件,实际使用时根据需要处理 20. private String uploadFile = /sdcard/testimg.jpg; 21. private String srcPath = /sdcard/testimg.jpg; 2
4、2. / 服务器上接收文件的处理页面,这里根据需要换成自己的 23. private String actionUrl = http:/10.100.1.208/receive_file.php; 24. private TextView mText1; 25. private TextView mText2; 26. private Button mButton; 27. 28. Override 29. public void onCreate(Bundle savedInstanceState) 30. 31. super.onCreate(savedInstanceState); 32
5、. setContentView(R.layout.main); 33. 34. mText1 = (TextView) findViewById(R.id.myText2); 35. mText1.setText(文件路径:n + uploadFile); 36. mText2 = (TextView) findViewById(R.id.myText3); 37. mText2.setText(上传网址:n + actionUrl); 38. /* 设置mButton的onClick事件处理 */ 39. mButton = (Button) findViewById(R.id.myBut
6、ton); 40. mButton.setOnClickListener(new View.OnClickListener 41. 42. Override 43. public void onClick(View v) 44. 45. uploadFile(actionUrl); 46. 47. ); 48. 49. 50. /* 上传文件至Server,uploadUrl:接收文件的处理页面 */ 51. private void uploadFile(String uploadUrl) 52. 53. String end = rn; 54. String twoHyphens = -;
7、 55. String boundary = *; 56. try 57. 58. URL url = new URL(uploadUrl); 59. HttpURLConnection httpURLConnection = (HttpURLConnection) url 60. .openConnection; 61. / 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃 62. / 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。 63. httpURLConnection.setChunkedStreamingMode(128 * 1024);/
8、128K 64. / 允许输入输出流 65. httpURLConnection.setDoInput(true); 66. httpURLConnection.setDoOutput(true); 67. httpURLConnection.setUseCaches(false); 68. / 使用POST方法 69. httpURLConnection.setRequestMethod(POST); 70. httpURLConnection.setRequestProperty(Connection, Keep-Alive); 71. httpURLConnection.setReque
9、stProperty(Charset, UTF-8); 72. httpURLConnection.setRequestProperty(Content-Type, 73. multipart/form-data;boundary= + boundary); 74. 75. DataOutputStream dos = new DataOutputStream( 76. httpURLConnection.getOutputStream); 77. dos.writeBytes(twoHyphens + boundary + end); 78. dos.writeBytes(Content-D
10、isposition: form-data; name=uploadedfile; filename= 79. + srcPath.substring(srcPath.lastIndexOf(/) + 1) 80. + 81. + end); 82. dos.writeBytes(end); 83. 84. FileInputStream fis = new FileInputStream(srcPath); 85. byte buffer = new byte8192; / 8k 86. int count = 0; 87. / 读取文件 88. while (count = fis.rea
11、d(buffer) != -1) 89. 90. dos.write(buffer, 0, count); 91. 92. fis.close; 93. 94. dos.writeBytes(end); 95. dos.writeBytes(twoHyphens + boundary + twoHyphens + end); 96. dos.flush; 97. 98. InputStream is = httpURLConnection.getInputStream; 99. InputStreamReader isr = new InputStreamReader(is, utf-8);
12、100. BufferedReader br = new BufferedReader(isr); 101. String result = br.readLine; 102. 103. Toast.makeText(this, result, Toast.LENGTH_LONG).show; 104. dos.close; 105. is.close; 106. 107. catch (Exception e) 108. 109. e.printStackTrace; 110. setTitle(e.getMessage); 111. 112. 113. 在AndroidManifest.xml文件里添加网络访问权限: plain view plain copy print? 1. 运行结果: