shen123ke 发表于 2013-1-15 03:00:52

文件上传

fileload.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>文件上传表单</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">
</head>

<body>
<html:form action="/upload" enctype="multipart/form-data">

          上传文件:<html:file property="uploadFile"></html:file>
<html:submit property="提交"></html:submit>
   
</html:form>
</body>
</html>


form:


package net.nbol.struts.form;

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

public class FileUploadForm extends ActionForm {
private FormFile uploadFile;

public FormFile getUploadFile() {
return uploadFile;
}

public void setUploadFile(FormFile uploadFile) {
this.uploadFile = uploadFile;
}



}




Action :


package net.nbol.struts.action;

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 net.nbol.struts.form.FileUploadForm;

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;

public class FileUploadAction extends Action {
/**
   * 上传的文件保存在服务器的路径
   */
private static String UPLOAD_FILE_PATH = "F:/skh/pic/";
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
      FileUploadForm uploadForm = (FileUploadForm)form;
      //得到上传的文件
      FormFile uploadFile = uploadForm.getUploadFile();
      //得到文件名
      String fileName = uploadFile.getFileName();
      //得到文件大小
      int fileSize = uploadFile.getFileSize();
      System.out.println("FileName = " + fileName);
      System.out.println("FileSize=" + fileSize);
      boolean result = true;
      try{
            //得到文件的输入流
            InputStream is = uploadFile.getInputStream();
            //上传文件
            uploadFile(fileName,is);
      }catch(IOException ex){
            ex.printStackTrace();
            //假如上传文件失败,设置一个失败的标记位
            result = false;
      }
      if(result){
            return mapping.findForward("success");
      } else {
            return mapping.findForward("fail");
      }      
    }
   
    /**
   * 上传文件
   * @param fileName
   * @param is
   * @throws IOException
   */
    private void uploadFile(String fileName,InputStream is) throws IOException{
      OutputStream os = new FileOutputStream(UPLOAD_FILE_PATH + fileName);
      //8k缓存数据
      byte[] buffer = new byte;
      //设置读进缓存的字节数
      int len;
      while((len=is.read(buffer))!=-1){
            //将缓存数据写入磁盘
            os.write(buffer,0,len);
      }
      //关闭输出流
      os.close();
      //关闭输入流
      is.close();
    }


}


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="fileUploadForm" type="net.nbol.struts.form.FileUploadForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
      
    <action path="/upload" name="fileUploadForm" scope="request" type="net.nbol.struts.action.FileUploadAction">
          <forward name="success" path="/success.jsp" redirect="true"></forward>
          <forward name="fail" path="/fail.jsp" redirect="true"></forward>
          </action>
   

</action-mappings>

<message-resources parameter="net.nbol.struts.ApplicationResources" />
</struts-config>
页: [1]
查看完整版本: 文件上传