zoukankan      html  css  js  c++  java
  • wildfly jsf 文件 上传后 可以下载 访问

    //        String aa = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
    //        log.info("context path:" + aa);
    //
    //        ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
    //        String realPath = ctx.getRealPath("/");
    //        log.info("real root path:" + realPath);
    //        String apks = ctx.getRealPath("/apks");
    //        log.info("real apks path:" + apks);

    获取war的根路径。

    简单的方案是放到 wildfly的 jboss.server.data.dir 配置下。这样就可以保存了。

        File targetFile = null;
    
            try {
                InputStream stream = file.getInputstream();
    
                File uploads = new File(System.getProperty("jboss.server.data.dir"), Config.APK_UPLOAD_PATH);
                if (!uploads.exists()) {
                    uploads.mkdirs();
                }
                targetFile = new File(uploads, file.getFileName());
                Files.copy(stream, targetFile.toPath(),StandardCopyOption.REPLACE_EXISTING );
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    http访问可以使用servlet:

    @WebServlet(description = " ", urlPatterns = { "/download/*" })
    public class StbServlet extends HttpServlet {
        private static final long serialVersionUID = 100L;
        /**
         * @see HttpServlet#HttpServlet()
         */
        public StbServlet() {
            super();
        }
        
        protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
            String path = request.getPathInfo();
            ;
            String filename = path.substring(path.lastIndexOf('/')+1);
            File uploads = new File(System.getProperty("jboss.server.data.dir"), Config.APK_UPLOAD_PATH);
            if(!uploads.exists()){
                return;
            }
            
            File file = new File(uploads, filename);
            response.setHeader("Content-Type", getServletContext().getMimeType(filename));
            response.setHeader("Content-Length", String.valueOf(file.length()));
            response.setHeader("Content-Disposition", "inline; filename="" + filename + """);
            try {
                Files.copy(file.toPath(), response.getOutputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            processRequest(request, response);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            processRequest(request, response);
        }
        
    }

    更多配置和实现参考:

    http://stackoverflow.com/questions/4543936/load-images-from-outside-of-webapps-webcontext-deploy-folder-using-hgraphi

    http://stackoverflow.com/questions/18664579/recommended-way-to-save-uploaded-files-in-a-servlet-application

    http://stackoverflow.com/questions/14211843/how-to-save-uploaded-file-in-jsf

  • 相关阅读:
    作业2(4)求m和n之间的和
    作业2(3)计算x的n次方
    作业3(6)查询水果的单价。有 4 种水果,苹果(apples)、梨(pears)、桔子(oranges)和葡萄(grapes),
    作业3(5)输入五级制成绩(A-E),输出相应的百分制成绩(0-100)区间,要求使用 switch语句。
    作业3(4)循环输入多个年份 year,判断该年是否为闰年。判断闰年的条件是:能被 4 整除但不能被100 整除,或者能被 400 整除。输入-1退出程序执行
    P39-习题2-5
    P39-习题2-7
    计算N户电费
    P39-习题2-4
    P39-习题2-3
  • 原文地址:https://www.cnblogs.com/bigben0123/p/5527843.html
Copyright © 2011-2022 走看看