一、文件上传限制:

在web.xml中配置Struts前端控制器时,设置初始化参数:如下图所示

二、controller代码

 @Namespace("/")
@ParentPackage("struts-default")
@Scope("prototype")
@Controller
public class ImageAction extends ActionSupport { private File imgFile; public void setImgFile(File imgFile) {
this.imgFile = imgFile;
}
private String imgFileFileName; public void setImgFileFileName(String imgFileFileName) {
this.imgFileFileName = imgFileFileName;
} @Action(value = "imageAction_upload")
public String upload() throws IOException {
Map<String,Object> map = new HashMap<>();
try { String dirPath = "/upload/";
ServletContext servletContext = ServletActionContext.getServletContext();
String realPath = servletContext.getRealPath(dirPath); String suffix = imgFileFileName.substring(imgFileFileName.lastIndexOf("."));
String fileName = UUID.randomUUID().toString().replaceAll("-","")+suffix;
File destFile = new File(realPath+"/"+fileName); FileUtil.copyFile(imgFile,destFile); String contextPath = ServletActionContext.getServletContext().getContextPath(); map.put("error",0);
map.put("url",contextPath+dirPath+fileName); } catch (IOException e) {
map.put("error",1);
map.put("message",e.getMessage());
e.printStackTrace();
}
String json = JSONObject.fromObject(map).toString();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(json); return NONE;
}
}

SSH文件上传代码片段的更多相关文章

  1. servlet3.0获取参数与文件上传代码示例

    转: servlet3.0获取参数与文件上传代码示例 2018年08月26日 20:25:35 苏凯勇往直前 阅读数:98   package com.igeek.servlet;   import ...

  2. php文件上传代码解析

    php文件上传代码解析 is_uploaded_file()  //函数判断指定的文件是否是通过 HTTP POST 上传的,返回一个布尔值. $_FILES['upfile']['tmp_name' ...

  3. 实现Magento多文件上传代码功能开发

    在Magento中上传单个文件很简单,可以直接在继承的Mage_Adminhtml_Block_Widget_Form类中直接添加如下组件Field:  对于图片:   $fieldset->a ...

  4. (实用篇)php处理单文件、多文件上传代码分享

    php处理  单文件.多文件上传实例代码,供大家参考,具体内容如下 后台处理文件submit_form_process.php <?php /************************** ...

  5. PHP文件上传代码和逻辑详解

    文件上传的逐步完善------ [简单的上传:]   <form action="upload.php"  method="post"  enctype= ...

  6. PHP 图片文件上传代码

    通过 PHP,可以把文件上传到服务器.里面加入一些图片的判断,如果不加判断文件的类型就可以上传任意格式的文件. 为了网站的安全,肯定不让上传php文件,如果有人进入你的后台,上传了一个php文件,你的 ...

  7. PHP 图片文件上传代码分享

    分享下php上传图片文件的一段代码,挺不错的. 通过 PHP,可以把文件上传到服务器.加入一些图片的判断,如果不加判断文件的类型就可以上传任意格式的文件. 当然了,会禁止上传php文件,以及其它程序代 ...

  8. ASP文件上传代码

    在网上看到的代码,稍微有点问题,改了一下就可以了.Chrome下是可以用的,别的浏览器还没有确认. <% Response.Buffer = True Server.ScriptTimeOut= ...

  9. JavaWEB SSH文件上传

    一.提交表单的<form> method属性必须为post  并且添加enctype="multipart/form-data" 属性 前台: <td>上传 ...

随机推荐

  1. 【flask】 学习flask macro 模板

    首先 我是看着这个链接学习的 jinja2 macro官方文档 Flask开发中Macros的实践经验 Flask的Jinja2模板引擎 — 块与宏(6th) 在宏中 还可以赋值...用set标签.. ...

  2. springboot集成JsonRpc2.0

    导入依赖的jar: 配置AutoJsonRpcServiceImplExporter: 接口文件: 实现类: 测试:

  3. 启动express,端口被占用

    启动express项目报错: root@ubuntuServerVM:/home/nodejs/meadowlark/site# node meadowlark.jsevents.js:160 thr ...

  4. 浅谈postMessage跨域通信与localStorage实现跨域共享

    https://www.cnblogs.com/tyrion1990/p/8134384.html

  5. abp 依赖注入声明

    public class SchedulerManager : ISingletonDependency { private ILogger logger; public SchedulerManag ...

  6. dotnet publish

    发布Release版本:dotnet publish --configuration Release 发布Debug版本:dotnet publish --configuration Debug

  7. Homebrew下安装的软件自启动服务管理工具:Launchrocket

    帮助管理Homebrew安装的服务的软件,比如使用Homebrew安装的Mysql.Redis.MongoDB,传统方式需要使用命令行的命令,而使用LaunchRocket则可以在图形界面中进行管理. ...

  8. Jexus高级功能设置

    我们对服务器软件Jexus作了简单的介绍,同时我们也对Jexus的整体配置作了详细的讲解,介绍了Jexus的进程守护工具"jws.guard",相信各位读者对于Jexus应该已经有 ...

  9. (转)MySQL高可用架构之MHA

    MySQL高可用架构之MHA  原文:http://www.cnblogs.com/gomysql/p/3675429.html 简介: MHA(Master High Availability)目前 ...

  10. zendstudio 设置默认编码 utf-8 gbk

    1.Project > Properties > Resource 2.Window > Preferences > General > Workspace 3.Wind ...