文件上传的几种方式

来源:互联网 发布:手机防盗软件oppo 编辑:程序博客网 时间:2024/05/17 06:31

文件上传与文件上传一样重要。在Java中,要实现文件上传,可以有两种方式:

1、通过Servlet类上传

2、通过Struts框架实现上传

这两种方式的根本还是通过Servlet进行IO流的操作。

一、通过Servlet类上传

1、编写Sevlet类

package com.chanshuyi.upload;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@SuppressWarnings("serial")public class FileUploadServlet extends HttpServlet {  @Override  protected void service(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException {        InputStream in = request.getInputStream();        /* 设置文件保存地址 */    File saveFile = new File(this.getServletContext().getRealPath("/uploaded"), "hello.txt");    System.out.println("[文件保存地址]:" + saveFile.getAbsolutePath());        /* 保存 */    FileOutputStream out = new FileOutputStream(saveFile);    byte[] buf = new byte[4096];    int readLength = -1;    while((readLength = in.read(buf)) != -1)    {      out.write(buf);    }    out.flush();    out.close();    in.close();    response.getWriter().write("<html><script>alert('Uploaded Succeed!')</script></html>");  }}


这里用纯Servlet实现的时候,无法获取文件的文件名以及一些其他信息。 还不知道怎么解决(MARK)。

2、配置web.xml文件

添加以下代码:

1 <!-- 文件上传(通过Servlet实现)  -->2     <servlet>3         <servlet-name>fileUploadServlet</servlet-name>4         <servlet-class>com.chanshuyi.upload.FileUploadServlet</servlet-class>5     </servlet>6     <servlet-mapping>7         <servlet-name>fileUploadServlet</servlet-name>8         <url-pattern>/fileUploadServlet</url-pattern>9     </servlet-mapping>


3、前台代码

1 
<p>通过Servlet实现上传</p>2 <form action="fileUploadServlet" method="post" enctype="multipart/form-data"><input type="file" name="file" id="file"/><input type="submit" value="提交"/></form>

二、通过Struts框架实现上传

1、配置struts.xml文件

添加如下Action:

<!-- 文件上传(通过Struts实现) --><action name="fileUpload" class="com.chanshuyi.upload.FileUploadAction"></action>


2、编写Action类

 1 package com.chanshuyi.upload; 2  3 import java.io.File; 4  5 import javax.servlet.ServletContext; 6 import javax.servlet.http.HttpServletResponse; 7  8 import org.apache.commons.io.FileUtils; 9 import org.apache.struts2.ServletActionContext;10 11 import com.opensymphony.xwork2.ActionSupport;12 13 @SuppressWarnings("serial")14 public class FileUploadAction extends ActionSupport {15     16     /** ActionContext对象 **/17     ServletContext servletContext = ServletActionContext.getServletContext();18     19     HttpServletResponse response = ServletActionContext.getResponse();20     21     /* 特定的命名规则,不能改变 */22     /** 上传的文件,名字要与前台name属性相同 **/23     private File file; 24     25     /** 上传文件名称 **/26     private String fileFileName; 27     28     /** 上传文件类型 **/29     private String fileContentType;30     31     public String execute()throws Exception32     {33         if(file == null)34         {35             return null;36         }37         /* 设置文件保存地址 */38         File saveFile = new File(servletContext.getRealPath("/uploaded"), fileFileName);39         System.out.println("[文件保存地址]:" + saveFile.getAbsolutePath());40         if(!saveFile.getParentFile().exists())41         {42             saveFile.getParentFile().mkdir();43         }44         45         FileUtils.copyFile(file, saveFile);46         System.out.println("[系统消息]:文件已经保存,保存路径为->" + saveFile.getAbsolutePath());47         48         response.getWriter().write("<html><script>alert('uploaded Succeed!')</script></html>");49         50         return null;51     }52     /* 省略GET/SET方法 */53 }


3、前台页面

<p>通过Struts实现上传</p><form action="fileUpload.action" method="post" enctype="multipart/form-data"><input type="file" name="file" id="file"/><input type="submit" value="提交"/></form>本例写的Action处理后不返回result,直接向response对象写入数据,弹出上传成功的提示。 


0 0
原创粉丝点击