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
wildfly jsf 文件 上传后 可以下载 访问的更多相关文章
- Tomcat 8.5版本文件上传后无权限访问的问题
之前在tomcat 7下文件上传后访问一直没问题,现在tomcat版本升到8.5,在测试文件http上传时,发现所传文件无法通过nginx访问了. (Tomcat具体版本为8.5.11) PS:tom ...
- JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了。
JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了.因为tomc ...
- ASP.NET Core WEB API 使用element-ui文件上传组件el-upload执行手动文件文件,并在文件上传后清空文件
前言: 从开始学习Vue到使用element-ui-admin已经有将近快两年的时间了,在之前的开发中使用element-ui上传组件el-upload都是直接使用文件选取后立即选择上传,今天刚好做了 ...
- IE8+SpringMVC文件上传防止JSON下载
今天在IE8测试文件上传的时候发现总是提示下载,原因是上传接口返回的是json,通过以下修改就可以保证返回是json并且不会出现下载的情况: @RequestMapping(value = " ...
- spring mvc ajaxfileupload文件上传返回json下载问题
问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...
- node express formidable 文件上传后修改文件名
//我是用php的思想来学习nodejs var express = require('express'); var router = express.Router(); var fs = requi ...
- IE下文件上传, SCRIPT5: 拒绝访问 问题
最近遇到一个比较奇葩的问题,某些ie浏览器在页面中上传文件时,无法上传.查看控制台报错: SCRIPT5: 拒绝访问. jquery-3.2.1.min.js, 行4 字符5725 .并且我的最新版I ...
- from表单文件上传后页面跳转解决办法
from表单上传文件,路径跳转后,又不能转发回来. 本人的一个解决办法是.返回一段html代码,浏览器解析后后退一步,回到原来的页面并刷新. return "<html>< ...
- Servlet 起航 文件上传 中文文件名下载
@WebServlet(name = "ticketServlet",urlPatterns = {"/tickets"},loadOnStartup = 1) ...
随机推荐
- 从Nodejs脚本到vue首页看开源始末的DemoHouse
最近上Github看见了大漠的DemoHouse项目,看到Issues说准备做一个首页,于是我的第一想法就是做一个md列表页面,md文件可以很容易的生成一个html文件.刚刚做好脚本文件,可以生成li ...
- WebHeaderCollection 类
https://msdn.microsoft.com/zh-cn/library/system.net.webheadercollection(v=VS.95).aspx /// <summar ...
- Crowdsourcing(众包)
群众外包(英语:crowdsourcing)是互联网带来的新的生产组织形式.<连线>(Wired)杂志记者Jeff Howe于2006年发明的一个专业术语,用来描述一种新的商业模式,即企业 ...
- Android闹钟开发与展示Demo
前言: 看过了不少安卓闹钟开发的例子,都是点到为止,都不完整,这次整一个看看. 一.闹钟的设置不需要数据库,但是展示闹钟列表的时候需要,所以需要数据库: public class MySQLiteOp ...
- HEU KMS Activator v11.1.0 Windows激活
HEU KMS Activator基于MDL论坛的“KMS Server Emulator”,是一款KMS激活工具,为“知彼而知己”原创工具.主要适用于Windows以及Office的VL版本,无需联 ...
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- 顺序栈C语言实现
"` #include <stdio.h> #define MAXSIZE 10001 #define ELEMTYPE int #define STACK_EMPTY -999 ...
- springMVC+mybatis 增删该操作后判断影响行数一直返回-2147482646
MyBatis发现更新和插入返回值一直为"-2147482646"的错误是由defaultExecutorType设置引起的,如果设置为BATCH,更新返回值就会丢失.mybati ...
- ViewPager导航栏TabLayout
ViewPager中加入TabLayout导航 需要导入依赖包: 'com.android.support:appcompat-v7:xxxxx' compile 'com.android.supp ...
- Android 横屏不让输入法全屏显示
记录学习之用 查找资料参考记录的 在源码里进行修改. frameworks/base/core/Java/Android/inputmethodservice/InputMethodService. ...