日期:2014-05-17  浏览次数:20621 次

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中申明一个文件对象
然后其他操作文件方式和平时操作文件方式相同即可