当前位置: 代码迷 >> Java Web开发 >> struts2上传文件,该怎么处理
  详细解决方案

struts2上传文件,该怎么处理

热度:25   发布时间:2016-04-17 11:08:36.0
struts2上传文件
谁有完整地struts2上传文件代码,要详细地,要存到硬盘上的,谢谢

------解决方案--------------------
Java code
public String addUser() {        // 取得服务器上上传文件存放的路径        String str = ServletActionContext.getServletContext().getRealPath(                "/files/");        // 获得上传文件的完整路径        String FilePath = str + File.separator + fileFileName;        InputStream is = null;        OutputStream os = null;        try {            is = new FileInputStream(file);            os = new FileOutputStream(FilePath);            byte[] b = new byte[1024];            int len = 0;            while ((len = is.read(b)) != -1) {                os.write(b, 0, len);                os.flush();            }            is.close();            os.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        ss.addObject(user);        return "ha";    }
------解决方案--------------------
其实很简单的
action中申明一个文件对象
然后其他操作文件方式和平时操作文件方式相同即可
  相关解决方案