java 上传文件
1 public static boolean upload(File file, String savepath, String loginNo, 2 String filename) { 3 boolean mResult = false; 4 InputStream in = null; 5 OutputStream out = null; 6 try { 7 in = new BufferedInputStream(new FileInputStream(file)); 8 savepath = savepath + "/" + loginNo + "-" + filename; 9 out = new BufferedOutputStream(new FileOutputStream(savepath)); 10 byte[] buffer = new byte[1024]; 11 int len = 0; 12 while ((len = in.read(buffer)) > 0) { 13 out.write(buffer, 0, len); 14 } 15 mResult = true; 16 } catch (Exception e) { 17 mResult = false; 18 e.printStackTrace(); 19 } finally { 20 if (null != in) { 21 try { 22 in.close(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 } 27 if (null != out) { 28 try { 29 out.close(); 30 } catch (IOException e) { 31 e.printStackTrace(); 32 } 33 } 34 } 35 36 return mResult; 37 }
posted on 2014-10-23 16:24 hellofking 阅读(121) 评论(0) 编辑 收藏 举报