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

posted @   Bigben  阅读(499)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2015-05-25 通过HTTP协议实现多线程下载
点击右上角即可分享
微信分享提示