android - 格式错误的 URL 异常:空(Android 应用程序)
问题描述
在这里谦虚地寻求一些意见。我有一个将用户文件备份到托管服务器的 Android 应用程序。它已经好 3 个多月了,昨天它开始给出格式错误的 url 错误。我根本没有更改任何代码(请参见下面的代码),但它已经停止了。我试图输出错误是什么,它只返回 null
我的问题是 1) 是什么导致了这种变化(我怀疑我的虚拟主机做了一些现在让我很困惑的变化) 2) 我该如何解决这个问题?
我在我的应用程序中调用此代码:
sendFileToServer(uploadFilePath+uploadFileName,"http://www.blablabla/UploadToServerFileName.php");
实际代码:
public int uploadFile(String sourceFileUri) {
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer ;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
busy = true;
if (!sourceFile.isFile()) {
//dialog.dismiss();
Log.e("uploadFile", "Source File not exist :"
+ uploadFilePath + "" + uploadFileName);
return 0;
} else {
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
//something else to try 02022021 - add backbyuffersize and bytesavailable though
conn.setChunkedStreamingMode(1024);
//conn.setChunkedStreamingMode(1024);
conn.setRequestMethod("POST");//30122020
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
//runs here 08 03
//conn.setRequestProperty("Content-Length", Integer.toString(fileInputStream.available())); //12012021
// conn.setDoOutput(true);
conn.setRequestProperty("uploaded_file", fileName);//20012021
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);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available(); //02022021
bufferSize = Math.min(bytesAvailable, maxBufferSize); //02022021
bufferSize = 8192;
//buffer = new byte[bufferSize]; 02022021
buffer = new byte[8192];
// read file and write it into form...
// bytesRead = fileInputStream.read(buffer, 0, bufferSize);//02022021
bytesRead = fileInputStream.read(buffer);
while (bytesRead > 0) {
//dos.write(buffer, 0, bufferSize); 02022021
dos.write(buffer, 0, bytesRead);
bytesAvailable = fileInputStream.available();
//bufferSize = Math.min(bytesAvailable, maxBufferSize);02022021a
// bytesRead = fileInputStream.read(buffer, 0, bufferSize); 02022021a
bytesRead = fileInputStream.read(buffer);
}
// 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();
if (serverResponseCode == 200) {
itemcount++;
uploadcount++;
//editor.commit();
} else {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(UploadToServer.this, "Failed" + String.valueOf(serverResponseCode),
Toast.LENGTH_SHORT).show();
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
//dialog.dismiss();
ex.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
messageText.setText("MalformedURLException Exception :"+ex.getMessage());
Bugsnag.notify(new RuntimeException(String.valueOf(ex.getStackTrace())));
}
});
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
// dialog.dismiss();
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
// messageText.setText("Got Exception : see logcat ");
messageText.setText(e.getMessage());
// Toast.makeText(UploadToServer.this, "Got Exception : see logcat ",
// Toast.LENGTH_SHORT).show();
}
});
Log.e("Exception", "Exception : "
+ e.getMessage(), e);
}
// dialog.dismiss();
return serverResponseCode;
} // End else block
}
解决方案
推荐阅读
- c++ - 为什么在使用 boost rtree 最近查询时会出现编译错误?
- javascript - 此 Firebase onAuthStateChanged 如何在此代码中工作
- java - 如何在 AnyLogic 中选择通过网络中另一个代理连接的最近代理?
- javascript - 在 javascript 中从数据库中操作 CQL 参数
- inheritance - Odoo - 视图中的多级继承
- c++ - 下载的 libpqxx 源中的内联函数错误
- html - 背景图片在手机上没有响应
- r - 无法在 R 中的函数内操作全局/局部变量
- android - Discord 或 Telegram 如何向 Android 上的频道发送推送通知?
- c - 使用双指针,如何用字符串填充 2D?