seasar2的多文件上传

来源:互联网 发布:数车工艺品图纸及编程 编辑:程序博客网 时间:2024/05/16 12:28
备忘用。
JSP:
    <table border="0">
        <c:set var="fileCnt" value="0" />
  <c:forEach varStatus="unpan" begin="${fileCnt}" end="4">
         <tr>
          <th>文件上传</th>
             <td>
             <input type="file" name="gzUpfiles[${unpan.index}]" class="file" />
             </td>
         </tr>
  </c:forEach>
    </table>
Action:
public class Gg0201G01Action {
    @ActionForm
    @Resource
    protected Gg0201G01Form gg0201G01Form;
    @Resource
    protected Gg0201G01Logic gg0201G01Logic;
    /**
     * 実行メソッド
     *
     * @return
     */
    @Execute(validator = false)
    public String index() throws Exception {
        return "/gg/gg0201g01.jsp";
    }
    /**
     * 登録処理
     *
     * @return
     */
    @Execute(validator = false)
    public String regist() {
        /* 登録処理 */
        gg0201G01Logic.regist();
        return "/gg/gg0201G01.jsp";
    }
}
Form:

public class Gg0201G01Form {
    private static final long serialVersionUID = 1L;

    // アップロード用画像ファイル
    public FormFile[] gzUpfiles = {};
 
}

Logic:
public class Gg0101G02Logic {
    @Resource
    protected Gg0101G02Form gg0101G02Form;
 
    /**
     * 画像の登録処理
     *
     * @param workDir
     * @param uploadDir
     */
    public boolean imgRegExecute( ) {
        // ログ出力(処理開始)
        logger.debug("画像登録開始");
        // 登録ワークDirを取得する
        String work_reg_dir = "c://work";
        for (int Index = 0; Index < gg0201G01Form.gzUpfiles.length; Index++) {
            FormFile file = gzUpfiles[Index];
            if (file.getFileSize() > 0) {
                /* 画像登録処理 */
                try {
                    // 画像ファイルをワークに保存
                    bRtn = upLoad(file, work_reg_dir, "newFileName.jpg");
                    if (!bRtn) {
                        break;
                    }
                } catch (FileNotFoundException e) {
                    bRtn = false;
                    logger.error(e.getMessage(), e);
                    break;
                } catch (IOException e) {
                    bRtn = false;
                    logger.error(e.getMessage(), e);
                    break;
                }
            }
        }
        logger.debug("画像登録終了");
        return bRtn;
    }
    /**
     * 画像のアップロード処理
     *
     * @param file
     * @param uploadDir
     * @param newFileName
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    private boolean upLoad(FormFile file, String uploadDir, String newFileName) throws FileNotFoundException, IOException {
        boolean bRtn = true;
        File dirPath;
        try {
            /* ディレクトリ作成 */
            dirPath = new File(uploadDir);
            if (!dirPath.exists()) {
                dirPath.mkdirs();
            }
            // 書き込み
            UploadUtil.write(dirPath.getPath() + File.separator + newFileName, file);
        } catch (Exception e) {
            bRtn = false;
            logger.error(e.getMessage(), e);
        }
        return bRtn;
    }
原创粉丝点击