I'm trying to upload an image file (jpeg) to my test server using the method posted here a while ago:
try {
String pathToOurFile = path;
FileInputStream fileInputStream =
new FileInputStream(new File(pathToOurFile) );
BufferedInputStream bis = new BufferedInputStream(fileInputStream,3000);
byte[] bt=new byte[bis.available()];
HttpURLConnection connection =
(HttpURLConnection)new URL(string).openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.connect();
FileOutputStream input = (FileOutputStream) connection.getOutputStream();
input.write(bt);
bis.close();
} catch (MalformedURLException e) {
Context context = null;
Toast.makeText(context, "error in writing", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I'm getting an ClassCastException at this line: FileOutputStream input = (FileOutputStream) connection.getOutputStream(); thought Eclipse doesnt mark it as an error. How can I fix that?
PS the project is compiled against API lvl 17
The stream returned by getOutputStream()
is not a FileOutputStream
, so your cast is failing. Eclipse is not warning because by doing the cast, you are telling it that you know that the result will be a FileOutputStream
.
Just change the variable type to OutputStream
and remove the cast.
只需将FileOutputStream更改为OutputStream
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.