中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Struts2 上傳文件主要代碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
public File file; //與頁(yè)面上的 input file 標(biāo)簽的name 一樣
 public String name;

 

/**
  * 上傳文件公共方法
  * @param url 地址
  * @param ext 后綴名
  * @return 上傳成功
  */
 public boolean file(String url,String ext) {
  response = getResponse();

  InputStream input = null;
  long time = System.currentTimeMillis();
  OutputStream os = null;
  try {
   System.out.println(name);
   SystemParameter systemParameter = (SystemParameter) getRequest()
     .getSession().getServletContext()
     .getAttribute("systemParameter");
   String path = systemParameter.getFilePath() + url ;
   String houzui = name.substring(name.lastIndexOf("("), name.length());
   if (houzui.equals("(空白)")) {
    name=name.substring(0, name.indexOf("("));
   }
   if (name.equals("流程圖")) {
    name="liuchen";
   }
   // 檢查文件是否存在
   File f = new File(path, name+ ext);
   if (f.exists()) {
    System.out.println(f.getAbsolutePath());
    System.out.println(f.getName());
    String newName = path + name + "old_" + time
      + ext;

    System.out.println(newName);
    boolean is = f.renameTo(new File(newName));
    if (is == false) {
     f.renameTo(new File(path + name + ext));
    
     return false;
    } else {
     // 如果存在,在讀一次改過(guò)文件的名字,如果新的名字不存在,就提示上傳失敗
     File file = new File(newName);
     if (!file.exists()) {
      return false;
     }
    }
   }
   input = new FileInputStream(file);

   os = new FileOutputStream(f);
   byte[] by = new byte[1024];
   int length = input.read(by);
   while (length != -1) {
    os.write(by, 0, length);
    length = input.read(by);
   }
   return true;
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  } finally {
   try {
    if (input != null)
     input.close();
    if (os != null)
     os.close();
  } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:PHP將cvs導(dǎo)入到MySql

下一篇:python在windows鎖屏的代碼