Java file uploading painfully slow

若如初见. 提交于 2021-02-07 18:06:33

问题


I've built a small service which receives images from an Android device and saves them to a Amazon S3 Bucket. The code is pretty simple but is painfully slow. It goes like this:

public synchronized static Response postCommentPicture(Response response, Request request){
    JsonObject ret = new JsonObject();
    OutputStream outputStream;
    String meepId = request.params(":id");
    System.out.println("1");
    if(meepId == null){
        ret.addProperty("Error", "Missing meep id");
        response.body(ret.getAsString());
        return response;
    }
    try {
        System.out.println("2");
        //Chequeamos que vengan los datos del sender
        Map<String, String> urlData = Utils.splitQuery(request.queryString());
        if(!urlData.containsKey("senderName") || !urlData.containsKey("senderId"))
            throw new Exception("senderName or senderId missing");
        System.out.println("3");
        MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/temp");
        System.out.println("3.1");
        request.raw().setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement);
        System.out.println("3.2");
        Collection<Part> files = request.raw().getParts();
        System.out.println("3.3");
        if(files.size() == 0 || files.size() > 1){
            throw new Exception("No files or more than 1 file detected");
        }
        //Rest of the code...
    } catch (Exception e2){
        System.out.println(e2.getMessage());
        ret.addProperty("Error", e2.getMessage());          
    } finally {
        response.body(ret.toString());
        return response;
    }

So, as you can notice, I print logs on certain steps. The code runs smoothly until "3.2", where it begins to transfer the file from the client device. So, it takes some time to complete the transfer but, once it has finished uploading (as I can tell using Android Studio Network Monitor) the server takes 3 o 4 more minutes before it processes the next line and prints "3.3". The rest of the code runs smoothly as well and I can finally get a response client-side.

So, my question is why request.raw().getParts() takes up to 6 minutes, even when the upload has finished.


回答1:


It sounds as if the app which is sending the data from the Android end isn't closing the connection when it finishes sending the data.

This would result in both ends sitting there doing nothing until one of the read timeouts triggers.

Have you written the code on the Android device yourself, or are you using a browser or some third-party app?




回答2:


From client (android/ ios), try sending chunks of file, instead of sending the entire file. Server would receive those chunks, combine and create a file.

To make it even faster. From client send chunks using multiple threads.

or use some library for uploading like https://github.com/square/okhttp or https://github.com/koush/AndroidAsync



来源:https://stackoverflow.com/questions/35323838/java-file-uploading-painfully-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!