读书人

Struts1.x下的FormFile 如何修改上传文

发布时间: 2012-01-23 21:57:28 作者: rapoo

Struts1.x下的FormFile 怎么修改上传文件的名称?
如题 谢谢。。。 已经做到上传下载 数据库也正确 但是为了防止上传文件同名称不同内容所造成的覆盖 想问一下 麻烦各位同仁了

[解决办法]

Java code
FormFile file = myActionForm.getTheFile();        try {            InputStream input = file.getInputStream();            String path = request.getRealPath("/");            System.out.println(path + "/" + file.getFileName()); // 文件路径及名称            ByteArrayOutputStream baos = new ByteArrayOutputStream();            OutputStream out = new FileOutputStream(path + "/" + file.getFileName()); //这里将输出名称改一下就行了,不一定要用file.getFileName(),可以用自定义的名字            int i = 0;            byte[] buff = new byte[8192];            while ((i = input.read(buff, 0, 8192)) != -1) {                out.write(buff, 0, i);            }            out.close();            input.close();        } catch (Exception ex) {            System.out.println(ex.getMessage());        }
[解决办法]
Java code
 OutputStream out = new FileOutputStream(path + "/" + file.getFileName()); 

读书人网 >J2EE开发

热点推荐