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

📄 downfileaction.java

📁 文章管理系统 用Java开发,以Struts为框架,以Jdbc链接Mysql数据库。系统属性:系统属性设置、留言管理、友情链接管理、网站调查、公告管理 关于我们、版权声明、联系我们
💻 JAVA
字号:
package com.yhcms.manage.upload.action;

import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

import com.yhcms.utils.ReqUtils;
/**
 * <p>Title:系统下载附件</p>
 * <li>系统下载附件</li>
 * <br><b>CopyRight: yyhweb[由由华网]</b>
 * @author stephen
 * @version YH-2.0
 */
public class DownFileAction extends DownloadAction
{
    protected class ByteArrayStreamInfo implements StreamInfo
    {

        protected String contentType;
        protected byte bytes[];

        public String getContentType()
        {
            return contentType;
        }

        public InputStream getInputStream()
            throws IOException
        {
            return new ByteArrayInputStream(bytes);
        }

        public ByteArrayStreamInfo(String contentType, byte bytes[])
        {
            this.contentType = contentType;
            this.bytes = bytes;
        }

        public ByteArrayStreamInfo(String contentType, String str)
        {
            this.contentType = contentType;
            bytes = str.getBytes();
        }
    }

    protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        String contentType = "application/octet-stream";
        String fileNamePath;
        
        int artId = ReqUtils.getInt(request,"artId");
        String fileName;
        if(artId > 0)
            fileName = null;
        else
            fileName = ReqUtils.getString(request, "fileName");
        if(fileName == null)
            fileName = "yhcms.txt";
        fileNamePath = request.getRealPath("")+"/app/upload/" + fileName;
        int splashIndex = fileName.lastIndexOf("/");
        String saveName = splashIndex <= 0 ? fileName : fileName.substring(splashIndex + 1);
        response.setHeader("Content-disposition", "attachment; filename=" + saveName);
        
        File file = new File(fileNamePath);
        if(!file.exists())
            return new ByteArrayStreamInfo(contentType, "The file you request does not exist.");
        else
            return new FileStreamInfo(contentType, file);
    }
}

⌨️ 快捷键说明

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