Zip file from codenameone to Server

152 views
Skip to first unread message

jea...@gmail.com

unread,
Sep 19, 2017, 8:54:04 PM9/19/17
to CodenameOne Discussions
Hello I am working with Netbeans and Java8 right now I am with emulator for codenameone. what I try to do is send to the server one zip file I compress with codenameone (net.sf.zipme). The zip file works fine but as soon as I send it to the server and try to decompress I got the next error:  net.lingala.zip4j.exception.ZipException: Expected central directory entry not found (#1)

I think maybe is the way I sent the file because if I compress the file with codename one and move it manually it works fine, I mean I can unzip all the data. if I transfer the file I have got the error in the server.

I try this code to send the zip file to the server from codenameone

 MultipartRequest request = new MultipartRequest();
        request.setPost(true);
        document = FileSystemStorage.getInstance().openInputStream(path + zipFileName);
        documentBytes = Util.readInputStream(document);
        request.setUrl(ServerURLEnums.FileApi.getURLPath());
        request.addData(zipFileName, documentBytes, "text/html;charset=UTF-8");
        request.addRequestHeader("fileName", zipFileName);
        NetworkManager.getInstance().addToQueue(request);
        request.getResponseData();
        recursiveDelete(path + zipFileName);

and I have got this code in server side 

String fileName = request.getHeader("fileName");
        File saveFile = new File(SAVE_DIR + fileName);

        // prints out all header values
        Enumeration<String> names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String headerName = names.nextElement();
            System.out.println(headerName + " = " + request.getHeader(headerName));
        }
       
        // opens input stream of the request for reading data
        InputStream inputStream = request.getInputStream();

        // opens an output stream for writing file
        FileOutputStream outputStream = new FileOutputStream(saveFile);

        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;

        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        outputStream.close();
        inputStream.close();

        System.out.println("File written to: " + saveFile.getAbsolutePath());

        // sends response to client
        response.getWriter().print(fileName);


I save the file in the server but with that error. 


I was wondering if you can tell me the correct way to import that sort of documents to the server. 

Shai Almog

unread,
Sep 20, 2017, 1:02:12 AM9/20/17
to CodenameOne Discussions
Hi,
the multipart request sends a multipart upload which means the data is encoded during upload. You need a special type of servlet or server code to handle it. Just google multipart upload for a lot of samples on the subject.
Reply all
Reply to author
Forward
0 new messages