android 上传 服务器_Android
导读:在Android中上传文件到服务器,通常需要以下步骤: (图片来源网络,侵删) 1、获取文件路径 2、创建HttpURLConnection对象 3、设置请求属性 4、写入数据 5、读取响应 6、关闭连接 以下是详细的代码示例: i...
在Android中上传文件到服务器,通常需要以下步骤:
(图片来源网络,侵删)1、获取文件路径
2、创建HttpURLConnection对象
3、设置请求属性
4、写入数据
5、读取响应
6、关闭连接
以下是详细的代码示例:
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class UploadToServer {
// server url where the file will be posted
private static final String UPLOAD_URL = "http://yourserverurl.com/upload";
public void uploadFile(String sourceFileUri) {
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "r
";
String twoHyphens = "";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
Log.e("Huzzaman", "Source File not exist :" + sourceFileUri);
return;
}
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(UPLOAD_URL);
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", "KeepAlive");
conn.setRequestProperty("ENCTYPE", "multipart/formdata");
conn.setRequestProperty("ContentType", "multipart/formdata;
boundary=" + boundary);
conn.setRequestProperty("file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("ContentDisposition: formdata;
name="file";
filename="" + fileName + """ + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
// create a buffer of maximum size
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);
if (serverResponseCode == 200) {
// Toast message
Runnable completeMsg = () >
Toast.makeText(getActivity(), "File Upload Complete.", Toast.LENGTH_SHORT).show();
getActivity().runOnUiThread(completeMsg);
}
//close the streams //
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();
Log.e("Upload file to server", "Could not upload file: " + e.getMessage(), e);
}
}
}
在这个例子中,我们首先创建一个HttpURLConnection对象,然后设置请求属性,包括请求方法、连接类型、内容类型等,我们打开一个文件输入流,读取文件内容并写入HttpURLConnection的输出流,我们读取服务器的响应并关闭连接。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: android 上传 服务器_Android
本文地址: https://pptw.com/jishu/681568.html
