problem with writeObject in java

6

I'm uploading images to the server. The image is sent in an input file to my local server, I convert it into a byte array, I set the array in a model class (BasicFileAction) and then with an object of type URL I make a connection to a service in a server where I will save the image and send as a parameter the object of type BasicFileAction and it is already saved.

The problem is that when I load an image of more than 3.68MB the image does not upload and does not send me any error message, it does not enter the catch or anything similar. It seems that it thunders in

 ObjectOutputStream.writeObject(objaBasicFileAction);

I tried uploading many images, and EVERY image smaller than 3.68MB does upload it; I tried with images higher than 3.81MB and no longer uploads them. Images between 3.68MB and 3.81MB I could not get, so I do not know what exactly is the size that goes up and the one, from him, not.

My Model Class:

package com.mypackage.actions;

import java.io.Serializable;

public abstract class BasicFileAction implements Serializable{
    private int[] objcFileBytes;
    private String scFileName;

    public int[]  getFileBytes(){
        return objcFileBytes;
    }   

    public void setFileBytes( int[] objaFileBytes ){
        objcFileBytes = objaFileBytes;
    }   

    public String getFileName(){
        return scFileName;
    }   

    public void setFileName(String saFileName){
        scFileName = saFileName ;
    }   
    public abstract Object execute() throws Exception;

}

This is the method that makes the request:

public Object executeService(BasicFileAction objaBasicFileAction)
        throws Exception {
        URL objlURLServer = null;
        ObjectInputStream objlResponse = null;
        Object objlObjectResult = null;
        URLConnection objlURLConnection = null;
        ObjectOutputStream objlRequest = null;
        TunnelException exclTunnelException = null;

        try {
            objlURLServer = new URL((String) objcProperties.get(
                        FileRemoteHandler.SERVICE_NAME));
        } catch (MalformedURLException e) {
            throw new Exception(
                "No se puede efectuar la conexion al servidor de fotografias '" +
                (String) objcProperties.get(FileRemoteHandler.SERVICE_NAME) +
                "' : " + e.getMessage());
        }

        try {
            objlURLConnection = objlURLServer.openConnection();
            objlURLConnection.setDoOutput(true);
            objlURLConnection.setUseCaches(false);
            objlURLConnection.setRequestProperty("Content-Type",
                "application/octet-stream");
            System.out.println("terminando1");
            objlRequest = new ObjectOutputStream(new BufferedOutputStream(
                        objlURLConnection.getOutputStream()));

            try{
                objlRequest.writeObject(objaBasicFileAction); //De aquí no pasa con las imágenes mayores a 3.68MB

                objlRequest.flush();
                objlRequest.close();
            }catch(NotSerializableException r){
                System.out.println("1 "+r.getMessage());
            } catch(InvalidClassException t){
                System.out.println("2 "+t.getMessage());
            } catch(IOException w){
                System.out.println("3 "+w.getMessage());
            }

            // get the result input stream
            objlResponse = new ObjectInputStream(new BufferedInputStream(
                        objlURLConnection.getInputStream()));

            // read response back from the server
            objlObjectResult = objlResponse.readObject();

            if (objlObjectResult instanceof TunnelException) {
                exclTunnelException = (TunnelException) objlObjectResult;
                throw new Exception(exclTunnelException.getMessage());
            }

        } catch (Exception exclException) {
            System.out.println("here: "+exclException.getMessage());
            throw new Exception("Error al Ejecutar la Peticion : " +
                exclException.getMessage());
        }

        return objlObjectResult;
    }

Additionally I leave you information about the byte arrays of the different requests I made.

Those that were uploaded, the size of the array is in parentheses:

3.68MB (3867314) 
3.19MB (3355578) 
3.05MB (3204054) 

Those that did not get up:

 4.09MB (4293306)
 4.36MB (4572533)
 3.81MB (3997079)

I have reviewed the code in the image server, it is a servlet. I put a System.out.println at the beginning of the servlet to see if the servlet could be executed and I realized that I did not. It does not pass writeObject but it does not reach the servlet either.

Any ideas?

EDIT: I have updated the way I send the data. Imagining that the arrangement I sent him was too big, I split it in two. Now the BasicFileAction class has one more parameter: the second part of the array. I have programmed everything so that writeObject receives an object of type BasicFileAction, which, as I said before, has one more arrangement that is the second half of the byte array of the image. I tested the code and it works correctly in the same way with images that weigh 3.68MB or less. In the same way, it does not pass ObjectOutputStream.writeObject (objaBasicFileAction); And in the image server it does not print anything since the first line of the servlet is not executed.

With what I've done, I've noticed that the problem is the amount of information sent to the writeObject of java.io.ObjectOutputStream , even though the byte array is passed in many fixes of smaller sizes will continue to thunder.

What about writeObject? Is the information too large that you can not send it? Is there any different way to send the images?

    
asked by user2930137 18.08.2017 в 16:22
source

2 answers

2

Verify the following settings in tomcat: In $TOMCAT_HOME/conf/server.xml There is the tag <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/> change it for <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxPostSize="67589953" /> . That is, just add maxPostSize="67589953" is the maximum data size that can be sent per post. You can try to increase the size and the timeout.

Also go to the following path $TOMCAT_HOME/webapps/manager/WEB-INF/web.xml and there look for this section and try increasing the values:

<multipart-config> <!-- 52MB max --> <max-file-size>52428800</max-file-size> <max-request-size>52428800</max-request-size> <file-size-threshold>0</file-size-threshold> </multipart-config>

It is the maximum size to upload files. If you say that you do not reach the servlet where you saved the files, the problem should be where you upload the files. But I would still recommend you check those settings on the destination application server other than the source.

Another important thing, check the windows permissions of the folders where the files are saved, in such a way that system, your windows user (has network or not), and other users that have to see have permissions. I am not very experienced in this part, but I recommend asking your network administrator.

    
answered by 25.08.2017 / 17:11
source
1

Something like this:

OutputStream os = new ByteArrayOutputStream(); //Puede ser cualquier OutputStream.
ServletInputStream is       = request.getInputStream();
byte[] bufferData = new byte[1024];
int read=0;
while((read = is.read(bufferData))!= -1){
    os.write(bufferData, 0, read);
}
os.flush();
os.close();
is.close();
    
answered by 21.08.2017 в 18:11