Unity3D與JAVA服務器傳遞文件之服務器端
阿新 • • 發佈:2018-01-12
log seek ring pos con 文件中 utf 回車 服務
-
剛好工作中有用到,特此來記錄一下,JAVA服務器用的是JFinal框架。
- Unity上傳文件只能傳輸字節流,然後服務器這邊再將字節流寫入文件
-
public void uploadFile() throws IOException { renderNull(); //==================開始處理文件=================== //接收上傳文件內容中臨時文件的文件名 System.out.println(getRequest().getContentLength());
-
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服務器傳遞文件之服務器端