zoukankan      html  css  js  c++  java
  • Java文件上传的几种方式

    文件上传与文件上传一样重要。在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 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、前台页面

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

  • 相关阅读:
    块结构在文件中的表示IOB【转载】
    LSTM输入层、隐含层及输出层参数理解【转载】
    L3-002 特殊堆栈 (30 分) 模拟stl
    L1-006 连续因子 (20 分) 模拟
    L2-014 列车调度 (25 分)
    L3-021 神坛 (30 分) 计算几何
    P1156 垃圾陷阱 DP
    P1063 能量项链 区间dp
    P1040 加分二叉树 区间dp
    P1605 迷宫 dfs回溯法
  • 原文地址:https://www.cnblogs.com/chanshuyi/p/3850634.html
Copyright © 2011-2022 走看看