我想從SD卡上傳圖像和音頻文件到服務器。當運行沒有應用程序崩潰,但圖像和音頻上傳適當的方式。當我去服務器,並檢查圖像和音頻,並顯示文件沒有found.Means當我點擊音頻符號嘗試播放服務器上顯示文件未找到。Android DataOutputStream.write(byte [],int,int)'空對象引用
這裏是我的影像和音頻文件上傳方法
uploadFile(uploadFilePath+""+imagePath , imagePath);
public int uploadFile(String uploadFileName ,String upLoadImageAudioName)
{
String fileName = uploadFileName;
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String upLoadFilePath = baseDir+"/classnkk_images/";
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(uploadFileName);
if (!sourceFile.isFile())
{
Log.e("uploadFile", "Source File not exist :" +upLoadFilePath + "" + upLoadImageAudioName);
return 0;
}
else
{
try
{
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL("http://xxxx/xxxx/MobileService.svc/UploadFileStream/");
// 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
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);
if(fileName.endsWith(".png"))
{
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file_name\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// Code for sending the image....
dos.writeBytes(lineEnd);
}
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
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);
}
if(fileName.endsWith(".mp3"))
{
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file_name_audio\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// Code for sending the MP3
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
}
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200)
{runOnUiThread(new Runnable() {
public void run() {}
});
}
//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();
}
return serverResponseCode;
} // End else block
}
這裏是日誌信息,先進
09-23 05:25:45.178 23676-24640/com.example.tazeen.classnkk W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.DataOutputStream.write(byte[], int, int)' on a null object reference
09-23 05:25:45.178 23676-24640/com.example.tazeen.classnkk W/System.err﹕ at com.example.tazeen.classnkk.AddPost.uploadFile(AddPost.java:798)
09-23 05:25:45.178 23676-24640/com.example.tazeen.classnkk W/System.err﹕ at com.example.tazeen.classnkk.AddPost$7$1$1.run(AddPost.java:419)
感謝。
可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) – Henry