So here's the situation: I am able to successfully upload a file from a java swing application to my glassfish 4 server running a spring web application.
My problem is that it takes forever to upload!!! I understand that there are a lot of factors that goes into speed up/down on the web. I just want to make sure that my code isn't the slow point!
For my test file of 1.7mb, it takes on average about 14 seconds to upload. According to my ISP and verified by megapath speed test, i have a very consistent 50 MB/s connection both ways.
There seems to be a lot of different ways to achieve the same thing with respect to file uploads. I'm more of a UI developer and file xfer is new for me... I had a hard enough time getting the upload process to work.. i'm stumped on where to begin to troubleshoot/optimize this.
I've included the code that sends the file from the client as well as the the relevant method in the spring controller that handles the incoming file. (please note that I am sending meta data about the file in the form of JSON along with the file)
(client code, using apache's http client libraries)
CloseableHttpClient httpclient = HttpClients.createDefault();
long start = System.currentTimeMillis();
// request bean to JSON string
ObjectMapper mapper = new ObjectMapper();
String requestBeanAsJSON = mapper.writeValueAsString( requestBeanToSend );
HttpPost outgoingPostRequest = new HttpPost( serverURL );
FileBody fileBody = new FileBody( fileToSend, ContentType.DEFAULT_BINARY );
StringBody stringBody1 = new StringBody( "msg", ContentType.MULTIPART_FORM_DATA );
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode( HttpMultipartMode.BROWSER_COMPATIBLE );
builder.addPart( "upfile", fileBody );
builder.addPart( "text1", stringBody1 );
HttpEntity entity = builder.build();
outgoingPostRequest.setEntity( entity );
CloseableHttpResponse responseBody = httpclient.execute( outgoingPostRequest );
long end = System.currentTimeMillis();
System.out.println( "completed in " + ((end-start)/1000) + " seconds.");
(server code, spring, glassfish4)
@RequestMapping( method = RequestMethod.POST, value = "up" )
public @ResponseBody MyReturnObject handleFileUpload( @RequestParam( "file" ) MultipartFile file )
{
try
{
BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream( new File( FtConstants.getTempUploadBaseDir() + File.separator + uniqueToken + "." + FileUtils.getFileExtension( file.getOriginalFilename() ) ) ) );
FileCopyUtils.copy( file.getInputStream(), stream );
stream.close();
}
catch( Exception e )
{
}
MyReturnObject returnObject = new MyReturnObject();
returnObject.setToken( RandomStringUtils.randomAlphanumeric( 16 ));
return returnObject;
}
Is there anything glaring that I'm doing wrong? Like I said above, it works, but it's wicked slow.
Thanks in advance for any help!
Aucun commentaire:
Enregistrer un commentaire