Struts(文件上传下载实例)

Struts(文件上传下载实例) - 398198920 - 冰冻三尺非一日之寒Struts(文件上传下载实例) - 398198920 - 冰冻三尺非一日之寒

upload.jsp

<%@ page contentType="text/html;charset=GBK" language="java"%>
<%@ taglib uri="
http://struts.apache.org/tags-html" prefix="html"%>
<html>
 <head>
  <title>Struts File Upload Example</title>
 </head>
 <body bgcolor="white">
  <html:form action="/upload" method="post" enctype="multipart/form-data">
   <table>
    <tr>
     <td align="center" colspan="2">
      <font size="4">Please Enter the Following Details</font>
     </td>
    </tr>
    <tr>
     <td align="left" colspan="2">
      <font color="red"><html:errors /></font>
     </td>
    </tr>
    <tr>
     <td align="right">
      File Name
     </td>
     <td align="left">
      <html:file property="theFile" />
     </td>
    </tr>
    <tr>
     <td align="center" colspan="2">
      <html:submit>Upload File</html:submit>
     </td>
    </tr>
   </table>
  </html:form>
 </body>
</html>
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="uploadForm" type="cn.hxex.action.struts.form.UploadForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
       <action
      attribute="uploadForm"
      input="/upload.jsp"
      name="uploadForm"
      path="/upload"
      scope="request"
      type="cn.hxex.action.struts.action.UploadAction"
      validate="false">
      <forward name="download" path="/download.jsp" />
    </action>
    <action path="/download" type="cn.hxex.action.struts.action.DownloadFileAction" />
    <action path="/open" type="cn.hxex.action.struts.action.OpenFileAction" />


  </action-mappings>

  <message-resources parameter="cn.hxex.action.struts.ApplicationResources" />
</struts-config>

UploadAction.java

package cn.hxex.action.struts.action;

import java.io.File;
import java.io.FileOutputStream;

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

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 cn.hxex.action.struts.form.UploadForm;

public class UploadAction extends Action {
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  UploadForm uploadForm = (UploadForm) form;

  // Process the FormFile
  FormFile myFile = uploadForm.getTheFile();
  String contentType = myFile.getContentType();
  String fileName = myFile.getFileName();
  int fileSize = myFile.getFileSize();
  byte[] fileData = myFile.getFileData();
  System.out.println("contentType: " + contentType);
  System.out.println("File Name: " + fileName);
  System.out.println("File Size: " + fileSize);

  FileOutputStream out = new FileOutputStream(new File("c:\\" + fileName));
  out.write(fileData);
  out.close();

  HttpSession session = request.getSession();
  session.setAttribute("contentType", contentType);
  session.setAttribute("fileName", fileName);
  session.setAttribute("fileSize", Integer.valueOf(fileSize));

  return mapping.findForward("download");
 }

}

download.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html>
 <head>
  <title>Download and Open the file</title>
 </head>
 <body>
  <html:link action="/download">Download</html:link><br>
  <html:link action="/open">Open</html:link>
 </body>
</html>

DownloadFileAction.java

package cn.hxex.action.struts.action;

import java.io.File;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

public class DownloadFileAction extends DownloadAction {

 protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

  HttpSession session = request.getSession();
  String contentType = (String) session.getAttribute("contentType");
  String fileName = (String) session.getAttribute("fileName");

  // Set the content disposition
  response.setHeader("Content-disposition", "attachment; filename="
    + fileName);

  File file = new File( "c:\\" + fileName);
  return new FileStreamInfo(contentType, file);
 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值