📄 downfileaction.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 + -