?? 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 + -