Chinaunix首页 | 论坛 | 博客
  • 博客访问: 260288
  • 博文数量: 82
  • 博客积分: 2477
  • 博客等级: 大尉
  • 技术积分: 725
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-06 23:24
文章分类

全部博文(82)

文章存档

2013年(1)

2012年(3)

2011年(15)

2010年(36)

2009年(27)

分类: Java

2010-03-25 09:58:48

package com.htmltaglibs.struts.action;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.htmltaglibs.struts.form.HtmlFileForm;

/**
 * MyEclipse Struts Creation date: 03-24-2010
 *
 * XDoclet definition:
 *
 * @struts.action path="/htmlFile" name="htmlFileForm"
 *                input="/form/htmlFile.jsp" scope="request" validate="true"
 * @struts.action-forward name="success" path="/HtmlFile.jsp"
 */
public class HtmlFileAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  *
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

  HtmlFileForm hff = (HtmlFileForm) form;

//  String dir = servlet.getServletContext().getRealPath("/upload");
  String dir = "e:/upload";
  FormFile file = hff.getFile();

  if (file == null) {
   return mapping.findForward("success");
  }

  String fname = file.getFileName();
  String size = Integer.toString(file.getFileSize()) + "bytes";

  InputStream istream = file.getInputStream();
//  OutputStream ostream = new FileOutputStream(new String((dir + "/" + fname).getBytes("GBK"),"UTF-8"));
  OutputStream ostream = new FileOutputStream(dir + "/" + (new String(fname.getBytes("GBK"),"gb2312")));
  
  int bytesRead = 0;
  byte[] buffer = new byte[8192];
  while ((bytesRead = istream.read(buffer, 0, 8192)) != -1) {
   ostream.write(buffer, 0, bytesRead);
  }

  istream.close();
  ostream.close();

  hff.setFname(new String(fname.getBytes("GBK"),"gb2312"));
  hff.setSize(size);

  file.destroy();

  return mapping.findForward("success");
 }
}

阅读(1363) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~