上传文件呢

来源:互联网 发布:网络推广教程视频教程 编辑:程序博客网 时间:2024/05/18 01:04

文件上传步骤:
(1)首先创建一个web工程。
(2)在lib里面引入要使用的jar包
(3)在web.xml里面创建过滤器。
(4)src里面创建一个Action类继承了ActionSupport
(5)在要上传文件的jsp页面里的要上传文件的元素里定义一个name="upload(这个名字随便但是要与Action里面的名字相同,这样在Action里面才能接收到。)"
(6)在Action里面写一个属性 private File upload;并且有set和get方法。
(7)在上传文件的jsp页面里的form标签里面写上这么一段代码:

 enctype="multipart/form-data"

 没有这段代码的话将会出错。
 忘记了这段代码的话,可以到
 目录
  
 这个目录下面的default.properties里面去搜索saveDir就可以得到下面一段代码:
 
 ### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data
 # struts.multipart.parser=cos
 # struts.multipart.parser=pell
 struts.multipart.parser=jakarta
 # uses javax.servlet.context.tempdir by default
 struts.multipart.saveDir=
 struts.multipart.maxSize=2097152


 将 multipart/form-data  填到form表单里面的 enctype="multipart/form-data" 就可以了。
**************************************************************************************************************************************

本人自己敲打过的代码:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping></web-app>


struts.xml代码:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <package name="default" namespace="/" extends="struts-default">
        <action name="updemo" class="com.insigma.upAction" method="upLoad">
         <result name="success">/success.jsp</result>
         <result name="false">/login.jsp</result>
        </action>
    </package>
</struts>   

 

 

index.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
     <div align="center">
      <form action="<%=basePath%>updemo" method="post" enctype="multipart/form-data">
       <table>
        <tr> 
         <td>选择上传文件的路径:</td>
         <td><input type="file" value="ÉÏ´«Îļþ" name="upload" /></td>
        </tr>
        <tr> 
         <td>填写保存文件的路径:</td>
         <td><input type="text" /></td>
        </tr>
        <tr> 
         <td align="center" colspan="2"><input type="submit" value="ÉÏ´«"></td>
        </tr>
       </table>
      </form>
     </div>
  </body>
</html>

 

 

upAction.java代码如下:
package com.insigma;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;

public class upAction extends ActionSupport {
 private File upload;

 public File getUpload() {
  return upload;
 }

 public void setUpload(File upload) {
  this.upload = upload;
 }

 public String upLoad() {
  System.out.println("************************");
System.out.println(upload.isFile());
System.out.println(uploadContentType);
  File fileOut = new File("D://t.txt");//文件的写出:
  try {
   FileUtils.copyFile(upload,fileOut);//将内存里面的文件写出到fileOut路径下面。
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  return "success";
 }
}


目录如下:
 目录

*********************************************************************************************************************************
如果在upAction类里面设置上uploadFileName和uploadContenType那么就可以知道文件是什么名字和类型的。。。通常默认给你设置进去了。

upAction类里面的变化为:其它的没有发生变化:
package com.insigma;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;

public class upAction extends ActionSupport {
 private File upload;
 private String uploadFileName;
 private String uploadContentType;
 
 
 
 public String getUploadFileName() {
  return uploadFileName;
 }

 public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 }

 public String getUploadContentType() {
  return uploadContentType;
 }

 public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
 }

 public File getUpload() {
  return upload;
 }

 public void setUpload(File upload) {
  this.upload = upload;
 }

 public String upLoad() {
  System.out.println("************************");
System.out.println(upload.isFile());
System.out.println(uploadFileName);
System.out.println(uploadContentType);
  return "success";
 }
}
 
*************************************************************************************************************************

将上传文件上传到工程里面的根目录下面的文件夹里面:

自己敲打过的东东:
目录为:
 目录

要把上传的文件放到upload文件夹里面。。。

index.jsp的代码如下

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
     <div align="center">
      <form action="<%=basePath%>updemo" method="post" enctype="multipart/form-data">
       <table>
        <tr> 
         <td>Ñ¡ÔñÒªÉÏ´«Îļþ£º</td>
         <td><input type="file" value="ÉÏ´«Îļþ" name="upload" /></td>
        </tr>
        
        <tr> 
         <td align="center" colspan="2"><input type="submit" value="ÉÏ´«"></td>
        </tr>
       </table>
      </form>
     </div>
  </body>
</html>

 

upAction.java的代码如下:
package com.insigma;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;

public class upAction extends ActionSupport implements ServletContextAware{
 private File upload;
 private String uploadFileName;
 private String uploadContentType;
 
 private ServletContext context;
 
 
 

 public String getUploadFileName() {
  return uploadFileName;
 }

 public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 }

 public String getUploadContentType() {
  return uploadContentType;
 }

 public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
 }

 public File getUpload() {
  return upload;
 }

 public void setUpload(File upload) {
  this.upload = upload;
 }

 public String upLoad() {
 

   String path = context.getRealPath("/upload");
System.out.println(path);
System.out.println(uploadFileName);
  File fileOut = new File(path,uploadFileName);//ÎļþµÄд³ö
  try {
   FileUtils.copyFile(upload,fileOut);
   System.out.println(fileOut.isFile());
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  return "success";
 }

 public void setServletContext(ServletContext arg0) {
  context = arg0;
 }
}


struts.xml里面的代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <package name="default" namespace="/" extends="struts-default">
        <action name="updemo" class="com.insigma.upAction" method="upLoad">
         <result name="success">/success.jsp</result>
         <result name="false">/login.jsp</result>
        </action>
    </package>
</struts>  

 

自己所犯的错误:
一开始,自己完成了所有的操作,在进行是否文件已经传到文件里面验证时,直接到MyEclipse目录下面的项目名字里面的upload文件夹下面去找。。。找不到上传的文件。。。实际上应该到tomcat目录下面去找。。。因为发布的时候是要发布tomcat里面的内容。