我正在使用此代码将图像上传到我的网络服务器:
public void send (View view)
{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "sdcard/yo.jpg";
String urlServer = "http://192.168.1.4/uplaod.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
Log.i("ODPOWIEDZ", serverResponseMessage);
Context context = getApplicationContext();
Toast.makeText(context, "its here 1 ", Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Context context = getApplicationContext();
Toast.makeText(context, "its in catsh", Toast.LENGTH_LONG).show();
Log.i("WYJATEK", ex.getMessage());
}
}
我从LogCat收到此消息,这里的问题在哪里?
59-124 / system_process D / SntpClient:请求时间失败:java.net.SocketException:协议不支持的地址族
这是我的php文件
<?php
$target_path = "uploadedfile/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
}
else {
echo "There was an error uploading the file, please try again!";
}
?>
答案 0 :(得分:0)
SNTP
错误与时间协议有关,时间协议偶然与您的应用无关。看起来它只是模拟器试图从ntp
服务器上获取最新的时间戳。只是丢弃它。
这个问题仍然是另一回事。您确实注意到php
脚本的名称可能存在拼写错误?在您的代码中,您将其称为uplaod.php
。
此外,请确保您已在清单文件中启用了互联网访问权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>