已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
家用制氧机价格 家用制氧机价格 http Z Struts2 学习记录 文件上传 File Upload 使用使用 jsp Servlet 实现文件上传实现文件上传 在我们的 web 应用中 文件上传是一项非常常见的功能 以前我学习 php 的时候也用 php 写过文件上传 不过 php 中实现起来比较方 便 只需要从全局变量 FILES 中获取到上传文件的临时存放路径等信息 再把它拷贝到目标地址并重命名就可以了 在 Java 中要实 现文件上传要稍微复杂一点 我们需要通过 request 的 getInputStream 方法来获取到相关的输入流 然后在从输入流中读取文件内容 悲剧的就是在输入流中加入了一些信息 比如文件名之类的 所以我们要想从输入流中获取到纯正的文件内容 还需要我们做一些处理 比如我们有一个文本文件 里面只有一句话 hello word 那么如果我们在文件上传时直接将输入流中的数据写入到文件中 那么会多 出许多额外的信息 WebKitFormBoundaryEvAVDYPz4OMMZU92 Content Disposition form data name file filename 新建文本文档 txt Content Type text plain hello world WebKitFormBoundaryEvAVDYPz4OMMZU92 这些信息有些对我们有用 比如我们可以通过第二行来获取到上传文件的名字 第三行可以获取到文件的类型 但是实际文件的内容应 该只有 hello world 一行 也就是说前面的 5 行和后面的一行都是多余的 不应该写进文件 下面我们就对这些 多余 的信息做一些处理 首先我们新建一个 Servlet 来处理文件内容 java view plaincopyprint 1 public class AcceptServlet extends HttpServlet 2 3 public void doPost HttpServletRequest request HttpServletResponse response 4 throws ServletException IOException 5 6 获取输入流 7 ServletInputStream in request getInputStream 8 9 缓冲字节数组 10 byte b new byte 1024 11 读取第一个行信息 包含分隔符 12 int len in readLine b 0 b length 家用制氧机价格 家用制氧机价格 http Z 13 String f new String b 0 len 14 15 len in readLine b 0 b length 16 从第二行中提取文件名 17 String fileName new String b 0 len 18 19 fileName fileName substring 0 fileName lastIndexOf 20 fileName fileName substring fileName lastIndexOf 1 21 提取文件后缀 22 String suffix fileName substring fileName lastIndexOf 23 24 剔除两行无用信息 25 in readLine b 0 b length 26 in readLine b 0 b length 27 28 创建文件输出流 29 FileOutputStream out new FileOutputStream d 30 System currentTimeMillis suffix 31 32 读取数据 33 while len in readLine b 0 b length 1 34 String f1 new String b 0 len 35 如果已经到达结尾 则不写入 最后一行包含分隔符 36 if f1 contains f trim 37 break 38 39 out write b 0 len 40 41 42 out close 43 in close 44 45 PrintWriter p response getWriter 46 p print success 47 48 49 50 然后我们新建一个 input jsp 家用制氧机价格 家用制氧机价格 http Z 我们在浏览器中测试一下 提交之后 页面显示 success 我们到 d 盘下打开文件 查看文件内容 多次测试 每次选择不同的文件类型 比如图片 压缩文件 exe 程序 均能正确上传 使用使用 Struts2 实现文件上传实现文件上传 虽然我们在上面的例子中通过一些简单的处理 能够实现文件的上传 但是这有局限性 比如可能在 Linux 系统中或者不同的浏览器中 输入流中的附加信息的格式会有所不同 那么 Struts2 提供的文件上传功能将更加具有通用性和易用性 我们无需关注底层是何种实现 Struts2 会给我一个统一的结果 Struts2 中的文件上传功能是通过将 Commons FileUpload 开源库集成进来实现的 这个工作有 defaultStack 拦截器栈中的 fileUpload 拦截器来完成 我们查看文档可以知道该拦截器的实现类是 org apache struts2 interceptor FileUploadInterceptor 我们查看该拦截器的 intercept 方法的源码 java view plaincopyprint 1 public String intercept ActionInvocation invocation throws Exception 2 ActionContext ac invocation getInvocationContext 3 4 HttpServletRequest request HttpServletRequest ac get ServletActionContext HTTP REQUEST 5 6 if request instanceof MultiPartRequestWrapper 7 if LOG isDebugEnabled 家用制氧机价格 家用制氧机价格 http Z 8 ActionProxy proxy invocation getProxy 9 LOG debug getTextMessage struts messages bypass request new Object proxy getNamespace prox y getActionName ac getLocale 10 11 12 return invocation invoke 13 14 15 ValidationAware validation null 16 17 Object action invocation getAction 18 19 if action instanceof ValidationAware 20 validation ValidationAware action 21 22 23 MultiPartRequestWrapper multiWrapper MultiPartRequestWrapper request 24 25 if multiWrapper hasErrors 26 for String error multiWrapper getErrors 27 if validation null 28 validation addActionError error 29 30 31 if LOG isWarnEnabled 32 LOG warn error 33 34 35 36 37 bind allowed Files 38 Enumeration fileParameterNames multiWrapper getFileParameterNames 39 while fileParameterNames null 42 43 get the content type 44 String contentType multiWrapper getContentTypes inputName 45 46 if isNonEmpty contentType 47 get the name of the file from the input tag 48 String fileName multiWrapper getFileNames inputName 49 50 if isNonEmpty fileName 51 get a File object for the uploaded File 家用制氧机价格 家用制氧机价格 http Z 52 File files multiWrapper getFiles inputName 53 if files null 55 List acceptedContentTypes new ArrayList files length 56 List acceptedFileNames new ArrayList files length 57 String contentTypeName inputName ContentType 58 String fileNameName inputName FileName 59 60 for int index 0 index files length index 61 if acceptFile action files index fileName index contentType index inputName v alidation ac getLocale 62 acceptedFiles add files index 63 acceptedContentTypes add contentType index 64 acceptedFileNames add fileName index 65 66 67 68 if acceptedFiles isEmpty 69 Map params ac getParameters 70 71 params put inputName acceptedFiles toArray new File acceptedFiles size 72 params put contentTypeName acceptedContentTypes toArray new String acceptedContentTyp es size 73 params put fileNameName acceptedFileNames toArray new String acceptedFileNames size 74 75 76 else 77 if LOG isWarnEnabled 78 LOG warn getTextMessage action struts messages invalid file new Object inputName ac ge tLocale 79 80 81 else 82 if LOG isWarnEnabled 83 LOG warn getTextMessage action struts messages invalid content type new Object inputName ac getLocale 84 85 86 87 88 invoke action 89 return invocation invoke 90 家用制氧机价格 家用制氧机价格 http Z 从中我们大致可以看出 它首先会判断请求中是否包含文件上传 如果包含 那么就将这些要上传的文件转化为对应的 File 对象 并且 将这些文件对应的 ContentType 和 FileName 一并提取出来 它将这些对象设置到了请求参数中 由于 fileUpload 拦截器在 parameters 拦截器之前 因此它添加到请求中的这些参数将会被 parameters 拦截器设置到相应的 Action 类属性上 从源代码中我们也可以看出 对于文件上传 Action 中的一些属性的命名是被写死了 比如 ContentType 那么 Action 类中对应的属性名必须是表单中 file 表单的 name 属性值加上 ContentType 文件名也是表单 name 属性值加上 FileName 从源代码中我们也可以看出最后放入到请求参数中的都是数组类型 也就是 Struts2 也支持多文件的上传 那么对应的多文件上传时我 们 Action 类中属性也需要设置为数组类型 Struts2 能够自动将这些属性给我吗设置上 这个拦截器还有三个属性 通过设置这些属性 的值 可以给我吗简化一些操作 1 maximumSize 上传文件的最大长度 字节为单位 默认 2mb 2 allowedTypes 允许上传的文件内容的类型 个类型之间使用逗号隔开 3 allowedExtensions 允许上传的文件的后缀列表 多个用逗号隔开 单文件上传实例 java view plaincopyprint 1 public class UploadAction extends ActionSupport 2 3 private File attachment 4 private String attachmentContentType 5 private String attachmentFileName 6 7 public File getAttachment 8 return attachment 9 10 11 public void setAttachment File attachment 12 this attachment attachment 13 14 15 public String getAttachmentContentType 16 return attachmentContentType 17 18 19 public void setAttachmentContentType String attachmentContentType 20 this attachmentContentType attachmentContentType 21 22 23 public String getAttachmentFileName 24 return attachmentFileName 25 家用制氧机价格 家用制氧机价格 http Z 26 27 public void setAttachmentFileName String attackmentFileName 28 this attachmentFileName attackmentFileName 29 30 31 Override 32 public String execute throws Exception 33 34 if attachment null 35 File out new File d attachmentFileName 36 attachment renameTo out 37 38 39 return super execute 40 41 42 input jsp 测试 提交后打开上传后的文件 这里还遇到一点小问题 为了简便 我在 jsp 页面中只是使用了简单的 html 结果发现如果想在页面输出中文的话 那么这些中文全部 会编程问号 首先怀疑编码问题 在中加了 家用制氧机价格 家用制氧机价格 http Z 还是没效果 后来直接在页面中输入中文 提示无法保存保存中文 因为页面编码是 iso 8859
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 汉语言文学毕业论文格式要求及写作方法,汉语言文学毕业论文写作方法
- 中考作文之中考议论文满分作文600字
- 临床试验药物供应的供应商审计要点
- 中国矿业大学本科生毕业论文格式模版
- 海尔企业的SWOT分析
- 2025年试议煤炭企业的成本管理
- 南京信息工程大学本科生毕业论文设计任务书
- 2025~2026学年重庆市垫江县人教版(小升初)数学检测试卷【附解析】
- 答辩评语模版
- 汽车生产相关设备保全管理方法的改进研究
- 2026中国精准营养个性化定制服务商业模式可行性报告
- 4.1中国的机遇与挑战(课件)-2025-2026学年统编版道德与法治九年级下册
- 2025海南大华会计师事务所(特殊普通合伙)海南分所人才招聘笔试考试备考试题及答案解析
- 学术论文标准格式规范
- 2025年秋季川省成都国有企业招聘(纪检)练习题及答案
- 2026小红书营销IP通案
- 《化工企业可燃液体常压储罐区安全管理规范》(AQ3063-2025)对标检查表
- 债务处理委托协议书
- 鱼骨图分析方法培训
- 2025初一历史时间轴记忆手册
- 风机基础环防水施工方案
评论
0/150
提交评论