在Java中,要实现文件上传,可以有两种方式:

1、通过Servlet类上传

2、通过Struts框架实现上传

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

一、通过Servlet类上传

1、编写Sevlet类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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实现的时候,无法获取文件的文件名以及一些其他信息。还不知道怎么解决。

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 Exception
32     {
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、前台页面

1
2
<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对象写入数据,弹出上传成功的提示。

posted on 2017-03-13 14:09  Smoth  阅读(590)  评论(0编辑  收藏  举报