如何将文件上传到weed-fs

时间:2014-10-01 21:19:33

标签: java distributed weed-fs

您好我正在尝试使用weed-fs java client将文件上传到weed-fs。我想我已按照说明of how to install weed成功安装了杂草。总之我

  1. 使用命令$./weed master
  2. 启动主控
  3. 然后我的应用服务器$./weed volume -port=9444的卷服务器已经占用了端口8080
  4. 使用命令`$。/ weed upload -dir =" any_dir_with_small_files"
  5. 上传文件夹测试一切正常

    到目前为止一切都很顺利。 在我的应用程序中,我尝试从浏览器发布multipart/form-data,该浏览器由调用类来处理上传的servlet拦截。

        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());
                    }
                }
            }
        }
    }
    

    当我运行这个时,我得到了很多调试信息

        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]
    

    这就像这样继续像x10我屏幕的高度不要忘记我设置了一个非常小的字体大小。 有人可以告诉我应该怎么做。如果你可以指导我为杂草fs提供java客户端的正确指南,我也将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

好像你做得对。如果您不想看到它们,请关闭调试信息。