博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wildfly jsf 文件 上传后 可以下载 访问
阅读量:5172 次
发布时间:2019-06-13

本文共 3295 字,大约阅读时间需要 10 分钟。

//        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

转载于:https://www.cnblogs.com/bigben0123/p/5527843.html

你可能感兴趣的文章
String、ANSIString、PChar及TBytes之间的转换 BytesOf move stringof
查看>>
js中的特殊符号含义
查看>>
RedisTemplate和StringRedisTemplate的区别
查看>>
Linux下干净卸载mysql
查看>>
【转】TCP是流传输协议,UDP是包传输协议
查看>>
Javascript基础1
查看>>
SNF快速开发平台2019-权限管理模型实践-权限都在这里
查看>>
Shell脚本语言与编译型语言的差异
查看>>
错误:linker command failed with exit code 1 (use -v to see invocation)
查看>>
12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间
查看>>
css Reset
查看>>
js正则表达式大全
查看>>
多线程 NSThread GCD
查看>>
ZevenOS 5.0 发布,德国人的 Linux 发行
查看>>
pictureBox绑定Base64字符串
查看>>
postgre索引
查看>>
哈夫曼编码译码系统(c/c++)
查看>>
Hbase性能调优(一)
查看>>
5自由落体运动
查看>>
python数据处理的常用操作
查看>>