powered by Jive Software

Tracking file upload progress on Openfire[2]

How can I keep track of the uploading file when using httpUploadFile plugin ?
thanks

What do you mean exactly?

A client knows what file it is going to upload, so it can count the bytes that have already been sent. That should be usable to show some kind of progress indicator.

yeah right ,
but Im uploading a file and when I close the outputstream suddenly openfire server reset the connection and I ended up with no upload .I need to see how does the openfire deals with the upload from server side.

The upload is a fairly straightforward HTTP request. There’s nothing that Openfire needs to provide there apart from what’s normal for a HTTP request. I suspect that your client code has a bug.

Actually , Im using smack
and here is the code:

    private void upload(InputStream iStream, long fileSize, Slot slot, UploadProgressListener listener) throws IOException {
        final URL putUrl = new URL(slot.getPutUrl().toString()+"/logo_large.png") ;
        final XMPPConnection connection = connection();
        final HttpURLConnection urlConnection = createURLConnection(connection, putUrl);
        
        urlConnection.setRequestMethod("PUT");
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        System.out.println("fileSize "+fileSize);
        urlConnection.setFixedLengthStreamingMode(fileSize);
        urlConnection.setRequestProperty("Content-Type", "application/octet-stream");
        for (Map.Entry<String, String> header : slot.getHeaders().entrySet()) {
            urlConnection.setRequestProperty(header.getKey(), header.getValue());
        }

        final SSLSocketFactory tlsSocketFactory = this.tlsSocketFactory;
        if (tlsSocketFactory != null && urlConnection instanceof HttpsURLConnection) {
            HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
            httpsUrlConnection.setSSLSocketFactory(tlsSocketFactory);
        }

        try {
        	urlConnection.connect();
            OutputStream outputStream = urlConnection.getOutputStream();

            long bytesSend = 0;

            if (listener != null) {
                listener.onUploadProgress(0, fileSize);
            }

            BufferedInputStream inputStream = new BufferedInputStream(iStream);

            // TODO Factor in extra static method (and re-use e.g. in bytestream code).
            byte[] buffer = new byte[4096];
            int bytesRead;
            try {
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                    bytesSend += bytesRead;

                    if (listener != null) {
                        listener.onUploadProgress(bytesSend, fileSize);
                    }
                    System.out.println(bytesSend);
                }
            }catch (Exception e) {
            	e.printStackTrace();
			}
            finally {
				try {
					outputStream.close();
				} catch (Exception e) {
					LOGGER.log(Level.WARNING, "Exception while closing output stream", e);
				}
                try {
                    inputStream.close();
                }
                catch (Exception e) {
                    LOGGER.log(Level.WARNING, "Exception while closing input stream", e);
                }
               
            }
            int status = urlConnection.getResponseCode();
            switch (status) {
            case HttpURLConnection.HTTP_OK:
            case HttpURLConnection.HTTP_CREATED:
            case HttpURLConnection.HTTP_NO_CONTENT:
                break;
            default:
                throw new IOException("Error response " + status + " from server during file upload: "
                                + urlConnection.getResponseMessage() + ", file size: " + fileSize + ", put URL: "
                                + putUrl);
            }
        }catch(Exception e) {
        	System.out.println("exception");
        	e.printStackTrace();
        }finally {
            urlConnection.disconnect();
        }
    }

it throws error : connection reset on statement :

int status = urlConnection.getResponseCode();

By the way , Im using the following library :

<dependency>
	<groupId>org.igniterealtime.smack</groupId>
	<artifactId>smack-experimental</artifactId>
	<version>4.2.1</version>
</dependency>

in the class

HttpFileUploadManager

I suspect that something is wrong with the code that does the upload, but I do not want to spend the time to review your code. Generally speaking, it’s often a lot easier to use some library to do the HTTP upload, instead of doing that yourself. Maybe you should consider using something like Apache’s HttpClient.

than you.
actually I did ,same problem

    private void apacheUpload(File file,URI url) throws Exception {
    	CloseableHttpClient httpclient = HttpClients.createDefault();
    	HttpPut put = new HttpPut(url);
    	FileEntity reqEntity = new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
    	put.setEntity(reqEntity);
    	HttpResponse response = null;
    	try {
			response = httpclient.execute(put);
		} catch (IOException e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
    	if(response == null) {
    		return ;
    	}
    	int responseCode = response.getStatusLine().getStatusCode();
    	if (responseCode - 200 >= 100)
    	     throw new Exception("bad status code: " + responseCode);
    	HttpEntity entity = response.getEntity();
    	String responseString = EntityUtils.toString(entity);
    	System.out.println("response");
    	System.out.println(responseString);
    }

error : Connection reset is thrown at statement :

response = httpclient.execute(put);