怎样实现文件上传的通用接口

来源:互联网 发布:网络微电影排行榜 编辑:程序博客网 时间:2024/05/01 21:52

1 接口:IFileUpload

@WebService

public interface IFileUpload {

    /**

     * 文件上传方法

     * @param in 输入字节流

     * @param filePath 文件上传路径

     * @param fileName 文件名称

     * @return 操作结果

     */

    public String upload(byte[] in, String filePath, String fileName);

}

2 接口实现:FileUploadImpl

public class FileUploadImpl implements IFileUpload {


    /**

     * 文件上传方法

     * @param in 输入字节流

     * @param filePath 文件上传路径

     * @param fileName 文件名称

     * @return 操作结果

     */

    @Override

    public String upload(byte[] in, String filePath, String fileName) {

        try {

            int len = 0;

            String path = this.getClass().getClassLoader().getResource("").getPath();

            //获得tomacatwebapp路径

            String pathtest = path.replace("lib", "webapps");

            String picRealPath = pathtest + filePath;

            //判断文件夹是否存在,不存在的话创建文件夹

            File filecheck = new File(picRealPath);

            if (!filecheck.exists()){

                filecheck.mkdirs();

            }// 检查上载文件的目录是否存在

            File df = new File(picRealPath + fileName);

            FileOutputStream out = new FileOutputStream(df,true);  

            len = in.length;

            out.write(in,0,len);

            out.close();

        } catch (Exception e) {

            e.getMessage();

        }

        return null;

    }

}



原创粉丝点击