前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java文件上传的几种方式

Java文件上传的几种方式

作者头像
陈树义
发布2022-04-29 10:53:07
2.6K1
发布2022-04-29 10:53:07
举报
文章被收录于专栏:陈树义陈树义

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

1、通过Servlet类上传

2、通过Struts框架实现上传

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

一、通过Servlet类上传

1、编写Sevlet类

代码语言:javascript
复制
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文件

添加以下代码:

代码语言:javascript
复制
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、前台代码

代码语言:javascript
复制
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:

代码语言:javascript
复制
<!-- 文件上传(通过Struts实现) -->
<action name="fileUpload" class="com.chanshuyi.upload.FileUploadAction"></action>

2、编写Action类 

代码语言:javascript
复制
 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、前台页面

代码语言:javascript
复制
<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对象写入数据,弹出上传成功的提示。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-08-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档