//        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. K-means算法和矢量量化

    语音信号的数字处理课程作业——矢量量化.这里采用了K-means算法,即假设量化种类是已知的,当然也可以采用LBG算法等,不过K-means比较简单.矢量是二维的,可以在平面上清楚的表示出来. 1. ...

  2. centos设置静态IP

    1.编辑网卡文件 vi /etc/sysconfig/network-scripts/ifcfg-eth0 # eth0为网卡编号 设置网卡eth0的IPV4信息,需要注意的是,设置的IPADDR需要 ...

  3. 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 10(排行榜界面&界面管理)

    继上一次介绍了<神奇的六边形>的完整游戏开发流程后(可点击这里查看),这次将为大家介绍另外一款魔性游戏<跳跃的方块>的完整开发流程. (点击图片可进入游戏体验) 因内容太多,为 ...

  4. WordPress使用固定链接

    WordPress安装后我们会发现,文章默认的url是很丑的,http://example.com/?p=N,其中N是文章ID,一串数字.默认链接在所有的环境下都运转良好,但和其他的类型比起来却没那么 ...

  5. C#实现清理系统内存

    金山内存整理工具.360内存清理工具非常好用,可以将系统内存最小化,提升系统运行速度.其实这些事情C#也可以做到,原理就是对系统进程中的进程内存进行逐个优化. 网上大多推荐使用系统的SetProces ...

  6. factor graph model

    主实验 文慧:用户,商品,评分,review,ranking. 数据集:数据规模,论文源代码

  7. ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”

    ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”,这在给定的上下文中无效 这是一个与Controller.F ...

  8. 十天冲刺---Day3

    站立式会议 站立式会议内容总结: git上Issues新增内容: 燃尽图 照片 组长情绪爆炸是很可怕的事情.这里自责一下. 进度缓慢是一件非常头疼的事情.还有每个人的时间都很紧张,除了学习,还有各种工 ...

  9. session超时设置

    session的设置方法有三种: 1.直接在应用服务器中设置 如果是Tomcat,可以在Tomcat目录下conf/web.xml中找到<session-config>元素,tomcat默 ...

  10. [转]Spring的事务管理难点剖析(1):DAO和事务管理的牵绊

    原文地址:http://stamen.iteye.com/blog/1441758 有些人很少使用Spring而不使用Spring事务管理器的应用,因此常常有人会问:是否用了Spring,就一定要用S ...