日期:2014-05-20  浏览次数:20666 次

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());