zoukankan      html  css  js  c++  java
  • SAE java应用读写文件(TmpFS和Storage)

    近期不少java用户都在提sae读写本地文件的问题,在这里结合TmpFS和Storage服务说说java应用应该如何读写文件
    TmpFS是一个供应用临时读写的路径,但请求过后将被销毁。出于安全考虑,sae限制了应用对本地IO操作,但本地操作肯定是存在的,所以sae提供了TmpFS来应对。如果需要将文件持久化怎么办呢?当然是使用storage。

    下面给出一个例子结合storage和TmpFS来写文件
    首先使用common-upload将文件上传至TmpFS下,之后再使用SaeStorage对象将文件存储至storage中。
    这里只是用于演示storage结合TmpFS使用,如果单纯使用storage服务大可不必这么麻烦,直接调用SaeStorage的write方法即可。

    public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("gbk");
            PrintWriter out = response.getWriter();
            // 使用SaeUserInfo拿到改请求可写的路径
            String realPath = SaeUserInfo.getSaeTmpPath() + "/";
            try {
                // 使用common-upload上传文件至这个路径中
                boolean isMultipart = ServletFileUpload.isMultipartContent(request);
                if (!isMultipart)
                    return;
                DiskFileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                upload.setFileSizeMax(1024 * 1024);
                List<FileItem> items = null;
                items = upload.parseRequest(request);
                for (FileItem item : items) {
                    if (!item.isFormField()) {
                        File fullFile = new File(item.getName());
                        File uploadFile = new File(realPath, fullFile.getName());
                        item.write(uploadFile);
                        // 上传完毕后 使用SaeStorage往storage里面写
                        SaeStorage ss = new SaeStorage();
                        // 使用upload方法上传到域为image下
                        ss.upload("image", realPath + fullFile.getName(),
                                fullFile.getName());
    
                        out.print("upload file:" + realPath + fullFile.getName()
                                + "</br>");
                    }
                }
                out.print("upload end...");
            } catch (Exception e) {
                out.print("ERROR:" + e.getMessage() + "</br>");
            } finally {
                out.flush();
                out.close();
            }
        }
  • 相关阅读:
    SQL 二进制和字符互转
    拒绝了对对象 'sp_OACreate' (数据库 'mssqlsystemresource',架构 'sys')的 EXECUTE 权限。
    SQL SERVER 正则替换
    SQL SERVER 算法执行效率
    webmin 安装
    MySQL5.6 PERFORMANCE_SCHEMA 说明
    flex 属性
    CSS 实现左侧固定,右侧自适应两栏布局的方法
    项目前端知识点
    为data中的某一个对象添加一个属性不起作用——this.$set的正确使用
  • 原文地址:https://www.cnblogs.com/x_wukong/p/3724648.html
Copyright © 2011-2022 走看看