⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileaction.java

📁 使用SSH三大框架
💻 JAVA
字号:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package web;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

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 org.apache.struts.upload.FormFile;
import org.apache.struts.util.ModuleException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import service.FileService;

/**
 * Struts Action
 * @author LuckyStar
 *
 */
public class FileAction extends DispatchAction
{
	/**
	 * 文件上传
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward upload(ActionMapping mapping, ActionForm form,
                            HttpServletRequest request,
                            HttpServletResponse response)
	{
		FileActionForm fileForm = (FileActionForm) form;
		FileService fileService = getFileService();
		fileService.save(fileForm);
		return mapping.findForward("forward");
	}
	/**
	 * 列出所有文件
	 */
	public ActionForward listAllFile(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
	throws ModuleException
	{
		FileService fileService = getFileService();
		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");
	}

	/**
	 * 文件下载
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws ModuleException
	 */
	public ActionForward download(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws ModuleException
    {
		FileActionForm fileForm = (FileActionForm) form;
		FileService fileService = getFileService();
		String fileName = fileService.getFileName(fileForm.getFileId());
		try
		{
			response.setContentType("application/x-msdownload");//下载文件的公共写法
			response.setHeader("Content-Disposition",
                           "attachment;" + " filename="+ new String(fileName.getBytes(), "UTF-8"));
			System.out.println("====download==");
			fileService.write(response.getOutputStream(), fileForm.getFileId());
		}
		catch (Exception e)
		{
			throw new ModuleException(e.getMessage());
		}
		return null;
    }

	/**
	 * 取得服务接口
	 * @return
	 */
	private FileService getFileService()
	{
		ApplicationContext appContext = WebApplicationContextUtils.
        	getWebApplicationContext(this.getServlet().getServletContext());
		return (FileService) appContext.getBean("fileService");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -