• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
nicesoft
博客园    首页    新随笔    联系   管理    订阅  订阅

struts2上传文件

    // 上传的文件
    private File file;
    private String fileContentType;// 文件类型
    private String fileFileName;// 文件名

    public void setFile(File file) {
        this.file = file;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

 


上传部份代码
        String moduleDir = "/upload";
        InputStream is;
        try {
            is = new FileInputStream(this.file);
            // 取文件后缀名
            String fileTail = this.fileFileName.substring(this.fileFileName
                    .lastIndexOf(".") + 1, this.fileFileName.length());
            // 以下获取当前时间的毫秒数
            long timeStamp = Calendar.getInstance().getTimeInMillis();

            map.put("pic", timeStamp + "." + fileTail);

            File saveFile = new File(ServletActionContext.getRequest()
                    .getRealPath(moduleDir + "/" + map.get("pic")));

            OutputStream os = new FileOutputStream(saveFile);
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = is.read(buf)) > 0) {
                os.write(buf, 0, length);
            }
            is.close();
            os.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

页面表单:

    <form method="post"
            action="<%=basePath%>admin/news/Update.do"  enctype="multipart/form-data">
            <input type="file"   name="file"  />

posted @ 2011-12-18 16:53  nicesoft  阅读(203)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3