How to upload a file to weed-fs

1.3k views Asked by At

Hi I am trying to upload a file to weed-fs using the weed-fs java client. I think I have successfully installed weed as per the instructions of how to install weed. In short I

  1. start the master with the command $./weed master
  2. then volume servers with $./weed volume -port=9444 my app server has already occupied port 8080
  3. test that everything is working by uploading a folder with the command `$./weed upload -dir="any_dir_with_small_files"

To this point all is well. In my app I try to post multipart/form-data from the browser which is intercepted by a servlet that calls a class to deal with the upload.

    public void upload(HttpServletRequest request) throws FileUploadException, IOException {
    client = new WeedFSClient(MASTER_ADDRESS, MASTER_PORT);
    if (ServletFileUpload.isMultipartContent(request)) {
        FileItemIterator fileItemIterator = new ServletFileUpload().getItemIterator(request);
        InputStream inputStream = null;
        while (fileItemIterator.hasNext()) {
            FileItemStream item = fileItemIterator.next();
            if (!item.isFormField()) {
                inputStream = item.openStream();
                File file = new File(request.getServletContext().getRealPath("/")+"img/uploads/"+item.getName());
                FileOutputStream fos = new FileOutputStream(file);
                Streams.copy(inputStream, fos, true);
                RequestResult result = client.upload(file);
                if (result.isSuccess()) {
                    System.out.println("uploaded file with ID "+result.getFid());
                }
            }
        }
    }
}

When I run this I get a lot of debug info

    Info:   DEBUG - Get connection for route {}->http://localhost:9444
Info:   DEBUG - Connecting to localhost:9444
Info:   DEBUG - CookieSpec selected: best-match
Info:   DEBUG - Auth cache not set in the context
Info:   DEBUG - Target auth state: UNCHALLENGED
Info:   DEBUG - Proxy auth state: UNCHALLENGED
Info:   DEBUG - Attempt 1 to execute request
Info:   DEBUG - Sending request: POST /2,ace132597a HTTP/1.1
Info:   DEBUG - >> "POST /2,ace132597a HTTP/1.1[\r][\n]"
Info:   DEBUG - >> "Transfer-Encoding: chunked[\r][\n]"
Info:   DEBUG - >> "Content-Type: multipart/form-data; boundary=TVf_3Jwe1DSTTHoUnZcyiA5KCVUFixPeItY3[\r][\n]"
Info:   DEBUG - >> "Host: localhost:9444[\r][\n]"
Info:   DEBUG - >> "Connection: Keep-Alive[\r][\n]"
Info:   DEBUG - >> "User-Agent: Apache-HttpClient/4.2.5 (java 1.5)[\r][\n]"
Info:   DEBUG - >> "[\r][\n]"
Info:   DEBUG - >> POST /2,ace132597a HTTP/1.1
Info:   DEBUG - >> Transfer-Encoding: chunked
Info:   DEBUG - >> Content-Type: multipart/form-data; boundary=TVf_3Jwe1DSTTHoUnZcyiA5KCVUFixPeItY3
Info:   DEBUG - >> Host: localhost:9444
Info:   DEBUG - >> Connection: Keep-Alive
Info:   DEBUG - >> User-Agent: Apache-HttpClient/4.2.5 (java 1.5)
Info:   DEBUG - >> "1088[\r][\n]"
Info:   DEBUG - >> "--TVf_3Jwe1DSTTHoUnZcyiA5KCVUFixPeItY3[\r][\n]"
Info:   DEBUG - >> "Content-Disposition: form-data; name="fileBody"; filename="23.jpg"[\r][\n]"
Info:   DEBUG - >> "Content-Type: text/plain[\r][\n]"
Info:   DEBUG - >> "[\r][\n]"
Info:   DEBUG - >> "[0xff][0xd8][0xff][0xe0][0x0][0x10]JFIF[0x0][0x1][0x1][0x1][0x0]H[0x0]H[0x0][0x0][0xff][0xe1][0x16][0xf2]Exif[0x0][0x0]II*[0x0][0x8][0x0][0x0][0x0][0x17][0x0][0x0][0x1][0x3][0x0][0x1][0x0][0x0][0x0][0x90][0x6][0x0][0x0][0x1][0x1][0x3][0x0][0x1][0x0][0x0][0x0][0x1a][0x4][0x0][0x0][0x2][0x1][0x3][0x0][0x3][0x0][0x0][0x0]"[0x1][0x0][0x0][0x6][0x1][0x3][0x0][0x1][0x0][0x0][0x0][0x2][0x0][0x0][0x0][0xf][0x1][0x2][0x0][0x6][0x0][0x0][0x0]([0x1][0x0][0x0][0x10][0x1][0x2][0x0][0x15][0x0][0x0][0x0].[0x1][0x0][0x0][0x12][0x1][0x3][0x0][0x1][0x0][0x0][0x0][0x1][0x0][0x0][0x0][0x15][0x1][0x3][0x0][0x1][0x0][0x0][0x0][0x3][0x0][0x0][0x0][0x1a][0x1][0x5][0x0][0x1][0x0][0x0][0x0]D[0x1][0x0][0x0][0x1a][0x1][0x5][0x0][0x1][0x0][0x0][0x0]L[0x1][0x0][0x0][0x1b][0x1][0x5]

this goes on like this for like x10 the height of my screen not forgetting I have set a very tiny font size. Can someone please tell me how this should be done. And I will also appreciate it if you can guide me to a proper guide to a java client for weed fs. Thanks in advance.

1

There are 1 answers

4
chrislusf On

Seems you are doing it right. Just turn off the debugging message if you do not want to see them.