码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D与JAVA服务器传递文件之服务器端

时间:2018-01-12 18:10:38      阅读:163      评论:0      收藏:0      [点我收藏+]

标签: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服务器传递文件之服务器端

标签:ext   i++   void   记录   poi   文件   getc   lin   file   

原文地址:https://www.cnblogs.com/wwwjie/p/8277062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!