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>

推荐文章

eclipse 打包插件fat jar

eclipse 打包插件fat jar

推荐文章

spring集成compass中出现的问题和总结

spring集成compass中出现的问题和总结

推荐文章

Managing Projects

Managing Projects

推荐文章

[ssh ][异常]The type org.springframework.dao.support.DaoSupport cannot be resolved........

[ssh ][异常]The type org.springframework.dao.support.DaoSupport cannot be resolved........

推荐文章

tomcat 6.0目录

tomcat 6.0目录

推荐文章

分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件

分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件

推荐文章

Java环境变量配置以及classpath详解

Java环境变量配置以及classpath详解

推荐文章

类初始化和类的加载问题

类初始化和类的加载问题

推荐文章

java -Duser.dir参数

java -Duser.dir参数

推荐文章

Tomcat6 一些调优设置内存和连接数

Tomcat6 一些调优设置内存和连接数

推荐文章

eclipse中配置struts2

eclipse中配置struts2

推荐文章

一个cocos2d小游戏

一个cocos2d小游戏

推荐文章

通过oracle,调用java类,并加载jar包到oracle中以支持java类

通过oracle,调用java类,并加载jar包到oracle中以支持java类

推荐文章

转:JNLP介绍

转:JNLP介绍

推荐文章

One-Jar之旅

One-Jar之旅

推荐文章

.classpath文件分析

.classpath文件分析