上传与下载

来源:互联网 发布:天津理工大学软件学院 编辑:程序博客网 时间:2024/04/29 09:06

package edu.web;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.ModuleException;

import edu.service.IFileService;

import java.io.*;
import java.util.List;

public class FileAction extends DispatchAction {
 IFileService fileService ;
 public ActionForward upload(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  FileActionForm fileForm = (FileActionForm) form;
  try{
   fileService.save(fileForm);
  }catch(org.springframework.jdbc.UncategorizedSQLException e){
   System.out.println("上传文件过大");
   return mapping.getInputForward();
  }
  return mapping.findForward("forward");
 }

 /**
  * 列出所有文件
  */
 public ActionForward listAllFile(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws ModuleException {
  System.out.println("here-----listAllFile--");
  
  List fileList = fileService.getAllFile();
  if (fileList != null) {
   System.out.println("is not null");
  } else {
   System.out.println("is null");
  }
  request.setAttribute("fileList", fileList);
  return mapping.findForward("fileList");
 }

 public ActionForward download(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws ModuleException {
  FileActionForm fileForm = (FileActionForm) form;
  
  String fileName = fileService.getFileName(fileForm.getFileId());
  try {
   response.setContentType("application/x-msdownload");
   response.setHeader("Content-Disposition", "attachment;"
     + " filename="
     + new String(fileName.getBytes(), "ISO-8859-1"));
   fileService.write(response.getOutputStream(), fileForm.getFileId());

//html头信息
  } catch (Exception e) {
   throw new ModuleException(e.getMessage());
  }
  return null;
 }

 public IFileService getFileService() {
  return fileService;
 }

 public void setFileService(IFileService fileService) {
  this.fileService = fileService;
 }


}

 

 

 

 

红色信息是重点