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

时间: 2024-08-27 18:39:40

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

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

前言: 从开始学习Vue到使用element-ui-admin已经有将近快两年的时间了,在之前的开发中使用element-ui上传组件el-upload都是直接使用文件选取后立即选择上传,今天刚好做了一个和之前类似的文件选择上传的需求,不过这次是需要手动点击按钮把文件上传到服务器中进行数据导入,而且最多只能够选择一个文件进行上传,上传成功后需要对file-list中的文件列表数据进行清空操作,在这里服务端使用的是ASP.NET Core WEB API来进行文件流数据接收和保存. 一.简单概述e

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

问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/addAnalysis", method = RequestMethod.POST) public void addAnalysisUI( HttpServletResponse response,HttpServletRequest request,HttpSession session, @Requ

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

//我是用php的思想来学习nodejs var express = require('express'); var router = express.Router(); var fs = require('fs'); var path= require("path"); var formidable = require('formidable'); /* GET home page. */ router.get('/', function(req, res, next) { res.

Android06_getpost提交_文件上传_多线程下载

提交数据有中文的话,一定要用URLEncoder进行编码 1,Get方式提交数据 1.1案例:发送QQ账号和密码 ①把信息通过网络请求发送到服务器 ②在服务端数据库查询账号密码是否存在 ③服务器返回具体的信息 1.1.1,Web端的实现 ①创建一个Servlet接收客户端请求 ②获取请求数据 ③封装成对象传入数据库中(因为主要是练习Android的网络请求,所以这里可以简化一下,直接判断两个数据是否相等) ④查询数据库返回结果 //通过response对象返回结果,response.getOut

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

from表单上传文件,路径跳转后,又不能转发回来. 本人的一个解决办法是.返回一段html代码,浏览器解析后后退一步,回到原来的页面并刷新. return "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/></head><body onload='JavaScript:history.

Hadoop之HDFS原理及文件上传下载源码分析(上)

HDFS原理 首先说明下,hadoop的各种搭建方式不再介绍,相信各位玩hadoop的同学随便都能搭出来. 楼主的环境: 操作系统:Ubuntu 15.10 hadoop版本:2.7.3 HA:否(随便搭了个伪分布式) 文件上传 下图描述了Client向HDFS上传一个200M大小的日志文件的大致过程: 首先,Client发起文件上传请求,即通过RPC与NameNode建立通讯. NameNode与各DataNode使用心跳机制来获取DataNode信息.NameNode收到Client请求后,

JavaWeb 后端 &lt;十四&gt; 文件上传下载

1.文件上传与下载 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 1.1 文件上传 文件上传,要点: 前台: 1. 提交方式:post 2. 表单中有文件上传的表单项: <input type=”file” /> 3. 指定表单类型: 默认类型:enctype="application/x-www-form-urlencoded" 文件上传类型:enctype =”multipart/

salesforce 零基础学习(四十二)简单文件上传下载

项目中,常常需要用到文件的上传和下载,上传和下载功能实际上是对Document对象进行insert和查询操作.本篇演示简单的文件上传和下载,理论上文件上传后应该将ID作为操作表的字段存储,这里只演示文件上传到Document对象中. 一.文件上传功能 apex代码 1 public with sharing class FileUploadUsedTransientController { 2 3 public transient Blob fileUploadBody{get;set;} 4

Java 文件上传与下载、email

1. 文件上传与下载 1.1 文件上传 文件上传,要点: 前台: 1. 提交方式:post 2. 表单中有文件上传的表单项: <input type="file" /> 3. 指定表单类型: 默认类型:enctype="application/x-www-form-urlencoded" 文件上传类型:multipart/form-data 手动实现文件上传 <body> <form name="frm_test"