I am uploading images to the server. I send the image 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 on a server where I will save the image and I send the object of type BasicFileAction as a parameter and it is already saved.
The problem is that when I upload an image of more than 3.68MB the image does not upload and it does not throw me any error message, it does not enter the catch or anything similar. It seems to thunder in
ObjectOutputStream.writeObject(objaBasicFileAction);
I tried uploading many images, and EVERY image smaller than 3.68MB does upload; I tried with images larger than 3.81MB and it no longer uploads them. Images between 3.68MB and 3.81MB I couldn't get, so I don't know exactly what size does go up and what size doesn't.
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 on the byte arrays of the different requests I made.
The ones that were uploaded, the size of the array is in parentheses:
3.68MB (3867314)
3.19MB (3355578)
3.05MB (3204054)
The ones that didn't upload:
4.09MB (4293306)
4.36MB (4572533)
3.81MB (3997079)
I have checked the code on the image server, it is a servlet. I put a System.out.println at the beginning of the servlet to see if the servlet was executed and well, I realized that it was not. It doesn't get past the writeObject but it doesn't get to the servlet either.
Any ideas?
EDIT: I have updated the way I send the data. Imagining that the arrangement I was sending 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 array that is the second half of the byte array of the image. I tested the code and it works fine AS WELL with images that are 3.68MB or less. Likewise, it does not go beyond ObjectOutputStream.writeObject(objaBasicFileAction); And on the image server it doesn't print anything because not even the first line of the servlet is executed.
With what I have done, I have realized that the problem is the amount of information that is sent to the writeObject of java.io.ObjectOutputStream , even if I pass the byte array to it in many smaller size arrays, it will continue to crash.
What about writeObject? Is the information too big that you cannot send it? Is there a different way to send you the images?
Check the following configurations in tomcat: In
$TOMCAT_HOME/conf/server.xml
The tag exists,<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
change it to<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxPostSize="67589953" />
. In other words, just addmaxPostSize="67589953"
it to the maximum size of data that can be sent per post. You can try increasing the size and 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 for uploading files. If you say that it does not reach the servlet where you will save the files, the problem must be where you upload the files. But I would still recommend checking those settings on the destination application server apart from the source one.
Another important thing, check the windows permissions of the folders where the files will be saved, in such a way that system, your windows user (networked or not), and other users that have to do with it have permissions. I am not very expert in this part, but I recommend you ask your network administrator.
Something like that: