File upload using java websocket API and Javascript

后端 未结 2 1349
野的像风
野的像风 2020-12-13 21:53

I\'m studying websocket and have done chat program with websocket/json. But I\'m stuck at file uploading ATM. Any advice & answer would be thankful.

Serv

2条回答
  •  北海茫月
    2020-12-13 22:35

    After some researches and tries, I found out that 'reader.readAsBinaryString(file);' was a cause of Question 1. Changing it into 'reader.readAsArrayBuffer(file);' my first problem has been solved.

    In addition, since websocket transfers a file as several partial data automatically, I changed source as below. This works! only when the file size is not so big. :/

    Changed server side source:

    package websocket;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    
    import javax.websocket.CloseReason;
    import javax.websocket.EndpointConfig;
    import javax.websocket.OnClose;
    import javax.websocket.OnError;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    import javax.websocket.server.ServerEndpoint;
    
    @ServerEndpoint("/receive/fileserver")
    public class FileServer {
        static File uploadedFile = null;
        static String fileName = null;
        static FileOutputStream fos = null;
        final static String filePath="d:/download/";
    
        @OnOpen
        public void open(Session session, EndpointConfig conf) {
            System.out.println("chat ws server open");
        }
    
        @OnMessage
        public void processUpload(ByteBuffer msg, boolean last, Session session) {
            System.out.println("Binary Data");      
    
            while(msg.hasRemaining()) {         
                try {
                    fos.write(msg.get());
                } catch (IOException e) {               
                    e.printStackTrace();
                }
            }
    
        }
    
        @OnMessage
        public void message(Session session, String msg) {
            System.out.println("got msg: " + msg);
            if(!msg.equals("end")) {
                fileName=msg.substring(msg.indexOf(':')+1);
                uploadedFile = new File(filePath+fileName);
                try {
                    fos = new FileOutputStream(uploadedFile);
                } catch (FileNotFoundException e) {     
                    e.printStackTrace();
                }
            }else {
                try {
                    fos.flush();
                    fos.close();                
                } catch (IOException e) {       
                    e.printStackTrace();
                }
            }
        }
    
        @OnClose
        public void close(Session session, CloseReason reason) {
            System.out.println("socket closed: "+ reason.getReasonPhrase());
        }
    
        @OnError
        public void error(Session session, Throwable t) {
            t.printStackTrace();
    
        }
    }
    

    Browser(Client) side:

    
    
    
    Chat
    
    
    
        

    File Upload

    Select file

    Still I cannot figure out how to transfer larger size file. (I'm suspecting auto-timeout and/or buffer size). Any advice plz?

提交回复
热议问题