07--struts--上传文件
来源:互联网 发布:平安科技java面试题 编辑:程序博客网 时间:2024/05/16 17:48
1.index.jsp
<form action="upload.do" method="post" enctype="multipart/form-data"> 标题:<input type="text" name="title"/><br/> 文件:<input type="file" name="myfile"/><br/> <input type="submit" value="上传"> </form>
说明:
(1)enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码。
application/x-www-form-urlencoded在发送前编码所有字符(默认)multipart/form-data不对字符编码。
在使用包含文件上传控件的表单时,必须使用该值。
text/plain空格转换为 "+" 加号,但不对特殊字符编码。(2)type---filefile定义输入字段和 "浏览"按钮,供文件上传。
2.ActionForm---UploadActionForm.java
package edu.tjpu.struts;import org.apache.struts.action.ActionForm;import org.apache.struts.upload.FormFile;public class UploadActionForm extends ActionForm {private String title;//文件采用FormFile声明private FormFile myfile;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public FormFile getMyfile() {return myfile;}public void setMyfile(FormFile myfile) {this.myfile = myfile;}}
说明:
(1)文件的上传收集采用FormFile来收集,FormFile是一个接口,里面定义了用于文件的各种方法。
(2)FormFile.java源码
package org.apache.struts.upload;import java.io.InputStream;import java.io.IOException;import java.io.FileNotFoundException;/** * This interface represents a file that has been uploaded by a client. It is * the only interface or class in upload package which is typically referenced * directly by a Struts application. */public interface FormFile{ /** * Returns the content type for this file. * * @return A String representing content type. */ public String getContentType(); /** * Sets the content type for this file. * * @param contentType The content type for the file. */ public void setContentType(String contentType); /** * Returns the size of this file. * * @return The size of the file, in bytes. */ public int getFileSize(); /** * Sets the file size. * * @param fileSize The size of the file, in bytes, */ public void setFileSize(int fileSize); /** * Returns the file name of this file. This is the base name of the file, * as supplied by the user when the file was uploaded. * * @return The base file name. */ public String getFileName(); /** * Sets the file name of this file. * * @param fileName The base file name. */ public void setFileName(String fileName); /** * Returns the data for the entire file as byte array. Care is needed when * using this method, since a large upload could easily exhaust available * memory. The preferred method for accessing the file data is * {@link #getInputStream() getInputStream}. * * @return The file data as a byte array. * * @exception FileNotFoundException if the uploaded file is not found. * @exception IOException if an error occurred while reading the * file. */ public byte[] getFileData() throws FileNotFoundException, IOException; /** * Returns an input stream for this file. The caller must close the * stream when it is no longer needed. * * @exception FileNotFoundException if the uploaded file is not found. * @exception IOException if an error occurred while reading the * file. */ public InputStream getInputStream() throws FileNotFoundException, IOException; /** * Destroys all content for the uploaded file, including any underlying * data files. */ public void destroy(); }
3.Action---TestUploadAction.java
package edu.tjpu.struts;import java.io.FileOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.upload.FormFile;public class TestUploadAction extends Action {@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {UploadActionForm uaf=(UploadActionForm)form;String tile=(String)uaf.getTitle();FormFile myfile=uaf.getMyfile();if(myfile!=null){FileOutputStream fos=new FileOutputStream("d:\\"+myfile.getFileName());fos.write(myfile.getFileData());fos.flush();fos.close();}return mapping.findForward("uploadfile");}}
说明:
(1)首先是类型转换为UploadActionForm;
(2)获取标题和文件--FormFile类型;
(3)通过getFileData()方法获取内容写入到磁盘。
4.struts-config.xml文件
<struts-config><form-beans><form-bean name="uploadForm" type="edu.tjpu.struts.UploadActionForm"></form-bean></form-beans><action-mappings><action path="/upload"type="edu.tjpu.struts.TestUploadAction"name="uploadForm"scope="request"><forward name="uploadfile" path="/upload_success.jsp"></forward></action></action-mappings></struts-config>
说明:
(1)这里的path 对应的index.jsp表达提交处理的地址;
(2)name对应于ActionForm中的名
(3)<forward>name属性值对应于Action返回的值
(4)交到upload_success.jsp去处理
(5)upload_success.jsp
<body> <h1>上传显示结果</h1> 标题:${uploadForm.title }<br/> 文件名:${uploadForm.myfile.fileName }<br/> </body>
要注意通过EL表达式输出时的格式。
总结:
(1)ActionForm中使用FormFile来接受上传的数据;
(2)在Action中使用FormFile接受从ActionForm中传递来的数据,并用数据流输出到磁盘;
(3)文件的上传可以通过<controller></controller>配置上传参数,比如maxFileSize、 nocache、 bufferSiz等。
- 07--struts--上传文件
- Struts的文件上传
- struts文件上传要点
- Struts的文件上传
- struts文件上传要点
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts上传文件笔记
- Struts文件上传
- Struts的文件上传
- 上传文件(struts篇)
- Struts的文件上传
- struts 多文件上传
- struts构建文件上传
- struts多文件上传
- Struts的文件上传
- 使用Struts上传文件
- vb 判断控件是否有焦点和控件类型
- 数据迁移solaris10x86到solaris10SPATC
- C#类
- binaryTree (draft)
- C#结构
- 07--struts--上传文件
- 属性和方法
- 链接外部的css或js文件名后带问号(?)参数的作用 .js? .css?
- 敢于面对 自己的 错误,能屈能伸
- C#变量
- Notepad++源码剖析(四)
- oracle存储过程中的if...elseif...else用法
- 查看Linux系统版本主要采用哪些方法
- ofono根文件目录分析,于0.52版