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

wildfly jsf 文件 上传后 可以下载 访问的更多相关文章

  1. Tomcat 8.5版本文件上传后无权限访问的问题

    之前在tomcat 7下文件上传后访问一直没问题,现在tomcat版本升到8.5,在测试文件http上传时,发现所传文件无法通过nginx访问了. (Tomcat具体版本为8.5.11) PS:tom ...

  2. JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了。

    JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了.因为tomc ...

  3. ASP.NET Core WEB API 使用element-ui文件上传组件el-upload执行手动文件文件,并在文件上传后清空文件

    前言: 从开始学习Vue到使用element-ui-admin已经有将近快两年的时间了,在之前的开发中使用element-ui上传组件el-upload都是直接使用文件选取后立即选择上传,今天刚好做了 ...

  4. IE8+SpringMVC文件上传防止JSON下载

    今天在IE8测试文件上传的时候发现总是提示下载,原因是上传接口返回的是json,通过以下修改就可以保证返回是json并且不会出现下载的情况: @RequestMapping(value = " ...

  5. spring mvc ajaxfileupload文件上传返回json下载问题

    问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...

  6. node express formidable 文件上传后修改文件名

    //我是用php的思想来学习nodejs var express = require('express'); var router = express.Router(); var fs = requi ...

  7. IE下文件上传, SCRIPT5: 拒绝访问 问题

    最近遇到一个比较奇葩的问题,某些ie浏览器在页面中上传文件时,无法上传.查看控制台报错: SCRIPT5: 拒绝访问. jquery-3.2.1.min.js, 行4 字符5725 .并且我的最新版I ...

  8. from表单文件上传后页面跳转解决办法

    from表单上传文件,路径跳转后,又不能转发回来. 本人的一个解决办法是.返回一段html代码,浏览器解析后后退一步,回到原来的页面并刷新. return "<html>< ...

  9. Servlet 起航 文件上传 中文文件名下载

    @WebServlet(name = "ticketServlet",urlPatterns = {"/tickets"},loadOnStartup = 1) ...

随机推荐

  1. [HDU5903]Square Distance(DP)

    题意:给一个字符串t ,求与这个序列刚好有m个位置字符不同的由两个相同的串拼接起来的字符串 s,要求字典序最小的答案. 分析:按照贪心的想法,肯定在前面让字母尽量小,尽可能的填a,但问题是不知道前面填 ...

  2. android之MP3播放器(1)

    该播放器只是对本地的MP3文件进行简单的播放 布局文件 布局文件中设置了三个按钮分别来进行播放.暂停和继续播放 <?xml version="1.0" encoding=&q ...

  3. springMVC学习--RESTful支持

    简介 RESTful架构,就是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用.RESTful(即Representational State T ...

  4. sqlite之聚合函数的使用

    聚合函数对一组值执行计算并返回单一的值.聚合函数对一组值执行计算,并返回单个值.除了 COUNT 以外,聚合函数都会忽略空值. 聚合函数经常与 SELECT 语句的 GROUP BY 子句一起使用. ...

  5. 在Ubuntu 14.04安装Nginx

    ###介绍 Nginx是世界上最流行的网络服务器中的一种,负责托管网络上一些流量最高的网站.在多数情况下,Nginx比Apache在资源上更加友好,可以作为网络服务器或反向代理服务器. 这篇教程中,我 ...

  6. HttpServletRequest 转换成MultipartHttpServletRequest

    //转换 HttpServletRequestMultipartHttpServletRequest mulReq=(MultipartHttpServletRequest)request;//获取上 ...

  7. [转]JSON 入门指南

    原文地址:http://www.ibm.com/developerworks/cn/web/wa-lo-json/ 尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web ...

  8. GIF/PNG/JPG和WEBP/base64/apng图片优点和缺点整理

    GIF/PNG/JPG/WEBP/APNG都是属于位图(位图 ,务必区别于矢量图): GIF/PNG和JPG这三种格式的图片被广泛应用在现今的互联网中,gif曾在过去互联网初期慢速的情况下几乎是做到了 ...

  9. shell截取字符串的方法

    参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...

  10. js-JavaScript高级程序设计学习笔记11

    依然第十四章 1.选择部分文本:使用setSelectionRange()方法,两个参数为第一个字符的索引和最后一个字符之后的索引,类似于substring()方法. 2.IE8及更早版本使用范围选择 ...