struts2显示服务器图片,struts2 上传图片到服务器

web 应用下上传文件需要为表单设置 enctype multipart form data 属性 表单将以二进制 编码的方式提交请求 然后由解析器进行解析 struts2 不提供解析器 但可以和 common fileupload COS 很好的结合 struts2 默认使用 Jakarta 的 common fileupload 文件上传框架 在 struts2 core jar 中 default properties 中可见 struts multipart parser jakarta 下面展示一个使用 common fileupload 实现文件上传的示例 1 从 http commons apache org 上下载 commons fileupload 1 2 1 jar 和 commons io 1 3 2 jar 然后放到 WEB INF lib 文件夹下 2 编写代码 upload input jsp xhtml view plaincopy 1 2 3 4 5 6 7 文件上传 8 9 10 11 12 标题 13 14 文件 15 16 17 18 19 upload jsp xhtml view plaincopy 1 3 4 5 6 7 文件上传 8 9 10 标题 title 11 12 文件 fileFileName 13 14 15 UploadAction java java view plaincopy 1 package com petrochina action business 2 3 import java io File 4 import java io FileInputStream 5 import java io FileOutputStream 6 import java io InputStream 7 import java io OutputStream 8 9 import mons io IOUtils 10 import org apache struts2 ServletActionContext 11 12 import com opensymphony xwork2 ActionSupport 13 14 public class UploadAction extends ActionSupport 15 16 private static final long serialVersionUID 1L 17 private File file 对应文件域的 file 封装文件内容 18 private String fileContentType 封装文件类型 19 private String fileFileName 封装文件名 20 private String savePath 保存路径 21 private String title 文件标题 22 23 Override 24 public String execute throws Exception 25 if file null 26 System out println getFileContentType 27 读取文件内容到 InputStream 里 28 InputStream is new FileInputStream getFile 29 创建输出流 生成新文件 30 OutputStream os new FileOutputStream getSavePath ge tFileFileName 31 将 InputStream 里的 byte 拷贝到 OutputStream 32 IOUtils copy is os 33 os flush 34 IOUtils closeQuietly is 35 IOUtils closeQuietly os 36 return SUCCESS 37 38 return INPUT 39 40 41 public File getFile 42 return file 43 44 45 public void setFile File file 46 this file file 47 48 49 public String getFileContentType 50 return fileContentType 51 52 53 public void setFileContentType String fileContentType 54 this fileContentType fileContentType 55 56 57 public String getFileFileName 58 return fileFileName 59 60 61 public void setFileFileName String fileFileName 62 this fileFileName fileFileName 63 64 65 public String getSavePath 66 将相对路径转换成绝对路径 67 return ServletActionContext getServletContext getRealPath savePath 68 69 70 public void setSavePath String savePath 71 this savePath savePath 72 73 74 public String getTitle 75 return title 76 77 78 public void setTitle String title 79 this title title 80 81 struts xml xhtml view plaincopy 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 image x ms bmp image jpeg image gif image png image x png application excel application vnd ms excel 24 25 2048000 26 27 28 29 test 30 31 WEB INF content business upload jsp 32 WEB INF content business upload input jsp 33 34 35 效果如下 上传页面 上传成功 项目结构图 可见 成功在 webapp test 下上传了 upload png 文件 在 struts xml 中配置 fileUpload 拦截器来限制上传文件的类型和大小时需要注意 1 通过 allowedTypes 参数限制文件类型时允许类型定义要全 比如 png 类型的图片就有 image png 和 image x png 两种类型 我开始只定义了 image png 发现 png 类型的图片 上传不了 后来看了 发现我上传的图片类型是 image x png 添加以后上传成功 所以遇到上传类型错误最好在本机输入看看是什么类型 然后在 allowedTypes 参数中添加 2 通过 maximunSize 参数限制上传的文件大小时 如果没有改变常量 struts multipart maxSize 的值 那么 maximunSize 就不要设置超过 2M 否则程序直接抛 出异常 拦截器不会拦截 因为 fileUpload 拦截器默认使用 struts multipart maxSize 的值 作为上传文件的最大值 当然 可以在 struts xml 或者 struts properties 中更改 struts multipart maxSize 的值 然后确保 maximunSize 参数的值不大于 struts multipart maxSize 的值 3 对于拦截器的报错信息可以在国际化资源文件中增加 struts messages error file too large your error message struts messages error content type not allowed your error message

展开阅读全文

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值