Unity3D與JAVA伺服器傳遞檔案之伺服器端

來源:互聯網
上載者:User

標籤:ext   i++   void   記錄   poi   檔案   getc   lin   file   

  • 剛好工作中有用到,特此來記錄一下,JAVA伺服器用的是JFinal架構。
    • Unity上傳檔案只能傳輸位元組流,然後伺服器這邊再將位元組流寫入檔案
    • public void uploadFile() throws IOException {        renderNull();                //==================開始處理檔案===================        //接收上傳檔案內容中臨時檔案的檔案名稱        System.out.println(getRequest().getContentLength());        if(getRequest().getContentLength()>0){        String tempFileName = new String("tempFileName.txt");        //tempfile 對象指向臨時檔案                File tempFile = new File(PathKit.getWebRootPath() + File.separator+tempFileName);        System.out.println("FilePath:"+PathKit.getWebRootPath() + File.separator+tempFileName);        //outputfile 檔案輸出資料流指向這個臨時檔案        FileOutputStream outputStream = new FileOutputStream(tempFile);        //得到用戶端提交的所有資料               InputStream fileSourcel = getRequest().getInputStream();                //將得到的用戶端資料寫入臨時檔案        byte b[] = new byte[1000];        int n ;        while ((n=fileSourcel.read(b))!=-1){                        System.out.println("b:"+b);            System.out.println("n:"+n);            outputStream.write(b,0,n);        }        //關閉輸出資料流和輸入資料流        outputStream.close();        fileSourcel.close();        //randomFile對象指向臨時檔案        RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");        //讀取臨時檔案的前三行資料        randomFile.readLine();        randomFile.readLine();        randomFile.readLine();        //讀取臨時檔案的第四行資料,這行資料中包含了檔案的路徑和檔案名稱        String filePath = randomFile.readLine();        //得到檔案名稱        System.out.println(filePath);        int position = filePath.lastIndexOf("filename");        String filename =Tool.codeString(filePath.substring(position+10,filePath.length()-1));        System.out.println("filename"+filename);        //重新置放讀取檔案指標到檔案頭        randomFile.seek(0);        //得到第五行斷行符號符的位置,這是上傳檔案資料的開始位置        long  fifthEnterPosition = 0;        int fifth = 1;        while((n=randomFile.readByte())!=-1&& fifth<=5)){            System.out.println("n:"+n);            System.out.println( fifth:" fifth);            if(n==‘\n‘){             fifthEnterPosition = randomFile.getFilePointer();             fifth++;            }            System.out.println( fifthEnterPosition:" fifthEnterPosition);        }        //產生上傳檔案的目錄        File fileupLoad = new File(PathKit.getWebRootPath() + File.separator,"upLoad");        fileupLoad.mkdir();        //saveFile 對象指向要儲存的檔案        File saveFile = new File(PathKit.getWebRootPath() +"\\upLoad",filename);        RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");        //找到上傳檔案資料的結束位置,即倒數第三行        randomFile.seek(randomFile.length());        long endPosition = randomFile.getFilePointer();        System.out.println("endPosition:"+endPosition);        int j = 1;        while((endPosition>=0)&&(j<=3)){            System.out.println("endPosition1:"+endPosition);            System.out.println("j:"+j);            endPosition--;            randomFile.seek(endPosition);            if(randomFile.readByte()==‘\n‘){                j++;            }        }        //從上傳檔案資料的開始位置到結束位置,把資料寫入到要儲存的檔案中        System.out.println( fifthEnterPosition:" fifthEnterPosition);        randomFile.seek fifthEnterPosition);        long startPoint = randomFile.getFilePointer();        System.out.println("startPoint:"+startPoint);        //endPosition=randomFile.getFilePointer();        System.out.println("endPosition:"+endPosition);        while(startPoint<endPosition){             System.out.println("startPoint1:"+startPoint);            //System.out.println("randomFile.readByte():"+randomFile.readByte());            randomAccessFile.write(randomFile.readByte());                        startPoint = randomFile.getFilePointer();        }        //關閉檔案輸入、輸出        randomAccessFile.close();        randomFile.close();        tempFile.delete();        //==================處理檔案結束===================        //向控制台輸出檔案上傳成功        System.out.println("File upload success!");        checkCsv();        }else{            renderText("false");        }            } 
    • public class Tool {    /** 檔案位元組流 */    public static byte[] getBytes(String filePath) {        byte[] buffer = null;        try {            File file = new File(filePath);            FileInputStream fis = new FileInputStream(file);            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);            byte[] b = new byte[1000];            int n;            while ((n = fis.read(b)) != -1) {                bos.write(b, 0, n);            }            fis.close();            bos.close();            buffer = bos.toByteArray();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return buffer;    }    /** 處理中文字串的函數 */    public static String codeString(String str) {        String s = str;        try {            byte[] temp = s.getBytes("UTF-8");            s = new String(temp);            return s;        } catch (UnsupportedEncodingException e) {            e.printStackTrace();            return s;        }    }
      }

       

    • 用網頁上傳與位元組流傳輸完全不一樣
    • public void uploadFileWeb() throws IOException {        UploadFile uploadFile = this.getFile();        String fileName = uploadFile.getOriginalFileName();        File file = uploadFile.getFile();        FileService fs = new FileService();        File t = new File("D:\\file\\" + fileName);         try {            t.createNewFile();        } catch (IOException e) {            e.printStackTrace();        }        fs.fileChannelCopy(file, t);        file.delete();    }
    • 下載檔案用的是JFinal的renderFile
    • // 下載檔案    public void downfile() throws FileNotFoundException, IOException {        readfiles("D:\\file\\");        String name = getPara("filename");        System.out.println(name);        int index = -1;        for (int i = 0; i < filearraylist.size(); i++) {            if (filearraylist.get(i).indexOf(name) != -1) {                index = i;            }        }        System.out.println(index);        if (index > -1) {            renderFile(new File(filearraylist.get(index)));            System.out.println(filearraylist.get(index));        }    }    // 遞迴擷取目錄下的所有檔案    public static boolean readfiles(String filepath) throws FileNotFoundException, IOException {        try {            File file = new File(filepath);            if (!file.isDirectory()) {                System.out.println("檔案");                System.out.println("path=" + file.getPath());                System.out.println("absolutepath=" + file.getAbsolutePath());                System.out.println("name=" + file.getName());                filearraylist.add(file.getPath());                // filearraylist.add("<a href=‘/file/downfile‘>下載</a><br>");            } else if (file.isDirectory()) {                System.out.println("檔案夾");                String[] filelist = file.list();                for (int i = 0; i < filelist.length; i++) {                    File readfile = new File(filepath + "\\" + filelist[i]);                    if (!readfile.isDirectory()) {                        System.out.println("path=" + readfile.getPath());                        System.out.println("absolutepath=" + readfile.getAbsolutePath());                        System.out.println("name=" + readfile.getName());                        filearraylist.add(readfile.getPath());                    } else if (readfile.isDirectory()) {                        readfiles(filepath + "\\" + filelist[i]);                    }                }            }        } catch (FileNotFoundException e) {            System.out.println("readfile()   Exception:" + e.getMessage());        }        return true;    }

       

Unity3D與JAVA伺服器傳遞檔案之伺服器端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.