首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts1的单文件下传

2012-09-03 
struts1的单文件上传//action的代码package com.actionimport java.io.Fileimport java.io.FileNotFound

struts1的单文件上传

//action的代码

package com.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.forms.FileForm;

public class FileAction extends DispatchAction {
 
 
 public ActionForward upload(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  FileForm formFile = (FileForm)form;
  
  String fileName = formFile.getFormFile().getFileName();
  int fileSize = formFile.getFormFile().getFileSize();
  //千万注意,得到的数据比较大,就会出现错误,因为,字节数组的大小小了
  //byte a [] = formFile.getFormFile().getFileData();
  //可以用另外一种方法
  saveFile(request, formFile, fileName, fileSize);
  System.out.println("曹欢");
  
  return null;
 }

 private void saveFile(HttpServletRequest request, FileForm formFile,
   String fileName, int fileSize) throws FileNotFoundException,
   IOException {
  if(!"".equals(formFile) && fileSize != 0)
  {
   InputStream iStream = formFile.getFormFile().getInputStream();
   String path = request.getSession().getServletContext().getRealPath("/images");
   //这是解决文件名乱码的问题
   String fileName1 = new String(fileName.getBytes("GBK"),"UTF-8");
   File file = new File(path, fileName1);
//   file.createNewFile();
   OutputStream oStream = new FileOutputStream(file);
   byte[] b = new byte[1024];
   int size = iStream.read(b);
   while (size>0) {
    oStream.write(b,0,size);
    size = iStream.read(b);
   }
   iStream.close();
   oStream.close();
  }
 }

}

 

//form的代码

package com.forms;

import java.io.Serializable;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class FileForm extends ActionForm implements Serializable{
 
 private FormFile formFile;

 public FormFile getFormFile() {
  return formFile;
 }

 public void setFormFile(FormFile formFile) {
  this.formFile = formFile;
 }

}

 

//filter的代码

package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {

 public void destroy() {
  // TODO Auto-generated method stub

 }

 public void doFilter(ServletRequest request, ServletResponse arg1,
   FilterChain arg2) throws IOException, ServletException {
  request.setCharacterEncoding("utf-8");
  arg2.doFilter(request, arg1);
 }

 public void init(FilterConfig arg0) throws ServletException {
  // TODO Auto-generated method stub

 }

}

 

//WEB-INF下的struts-config.xml的代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans>
   <form-bean name="fileForm" type="com.forms.FileForm"></form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
   <action path="/test" name="fileForm" parameter="p" input="" attribute="fileForm" scope="session" validate="false" type="com.action.FileAction"></action>
  </action-mappings>
  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

 

//web.xml的代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
   <filter-name>xxx</filter-name>
   <filter-class>com.filter.EncodingFilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>xxx</filter-name>
   <servlet-name>action</servlet-name>
   
  </filter-mapping>
</web-app>

 

 

热点排行