Tuesday, December 8, 2009

Upload file to Server


Upload the file to the server using the Http Connection. The File content is read as Input Stream and then uploaded to the server through the connections
public void sendFile(final String uri, final InputStream istream)
throws EvoxException
{
try
{
// Setup HTTP Connection
final URL evoxUrl = new URL(this.evoxServer + uri);
final HttpURLConnection connection =
 (HttpURLConnection) evoxUrl.openConnection();
connection.setRequestMethod(RequestType.POST.name());
connection.setDoInput(true);
connection.setDoOutput(true);
// Get Output Stream
final OutputStream ostream = connection.getOutputStream();
FileUtils.copyStreams(istream, ostream);
ostream.close();
final PrintWriter writer =
 new PrintWriter(connection.getOutputStream());
// Send File
writer.println(ostream);
writer.flush();
writer.close();
connection.getInputStream();
}
catch (IOException exception)
{
 throw new Exception("unable to send file: " + uri,
       exception);
}}
If the file is uploaded to the server make sure you close the connection if not needed, if not it will windup keeping lots of open connections in you applications




No comments:

Post a Comment