现在的位置: 首页 > 综合 > 正文

Struts上传文件笔记

2019年05月07日 ⁄ 综合 ⁄ 共 1251字 ⁄ 字号 评论关闭

<html:file property="picture"/>

FormFile formFile = form.getPicture();
   String picName = formFile.getFileName();
   if ( picName != null && picName.length() != 0 ) {
    String temp = picName.substring(picName.indexOf("."), picName.length()).toLowerCase();
    if ( !(".jpg".equalsIgnoreCase(temp)||".gif".equalsIgnoreCase(temp)||".jpeg".equalsIgnoreCase(temp))) {
     ActionErrors errors = new ActionErrors();
     errors.add( ActionMessages.GLOBAL_MESSAGE, new ActionMessage("enterprise.error.filetype"));
     this.saveErrors(httpServletRequest, errors);
     return actionMapping.getInputForward();
    }
    else {
     try {
      InputStream is = formFile.getInputStream();
      OutputStream out = new FileOutputStream (new StringBuffer(httpServletRequest.getRealPath("/")).append("/jsp/magazine/images/").append(picName).toString());
      int bytesRead = 0 ;
      byte[] buffer = new byte[8192];
      while ( (bytesRead = is.read(buffer,0,8192)) != -1 ) {
       out.write(buffer,0,bytesRead);
      }
      out.close();
      is.close();
     }
     catch ( FileNotFoundException fileNotFoundException ) {
      log.error(fileNotFoundException );
      return actionMapping.findForward("error");
     }
     catch ( IOException ioException ) {
      log.error(ioException );
      return actionMapping.findForward("error");
     }
    }
   }

抱歉!评论已关闭.