当前位置: 技术问答>java相关
怎样用程序限制用户只能用浏览器来下载东西
来源: 互联网 发布时间:2015-05-13
本文导语: 我开始是用http报头来判断,但是蚂蚁、Flashget这些东西可以更改http报头。怎样限制用户只能用浏览器,或者是限制他们用单线程也可以,我想用程序来实现 怎么办 | package com.shaosi.leon; import...
我开始是用http报头来判断,但是蚂蚁、Flashget这些东西可以更改http报头。怎样限制用户只能用浏览器,或者是限制他们用单线程也可以,我想用程序来实现 怎么办
|
package com.shaosi.leon;
import java.io.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
/**
* The object which perfoms the download.
* @author: Leon Zhao
*/
public class Download {
protected ServletContext m_application;
protected HttpServletRequest m_request;
protected HttpServletResponse m_response;
private boolean m_denyPhysicalPath;
private boolean m_forcePhysicalPath;
private String m_contentDisposition;
private Vector m_deniedFilesList;
private Vector m_allowedFilesList;
/**
* save in a virtual if it exists else consider the path as a physical if the porperty denyPhysicalPath is false.
*/
public static final int SAVE_AUTO = 0;
/**
* save the file only if the path is a virtual path.
*/
public static final int SAVE_VIRTUAL = 1;
/**
* save the file only if the path is a physical path.
*/
public static final int SAVE_PHYSICAL = 2;
/**
* open the file only if the path is a physical path.
*/
/**
* Download constructor comment.
*/
public Download() {
m_deniedFilesList = new Vector();
m_allowedFilesList = new Vector();
m_denyPhysicalPath = false;
m_forcePhysicalPath = false;
m_contentDisposition = new String();
}
/**
* Downloads a file.
* @param sourceFilePathName java.lang.String The full path to the file to download.
* @exception UploadException
* @exception IOException
* @exception ServletException
*/
public void downloadFile(String sourceFilePathName)
throws UploadException, IOException, ServletException {
downloadFile(sourceFilePathName, null, null);
}
/**
* Downloads a file.
* @param sourceFilePathName java.lang.String The full path to the file to download.
* @param contentType java.lang.String the content type.
* @exception UploadException
* @exception IOException
* @exception ServletException
*/
public void downloadFile(String sourceFilePathName, String contentType)
throws UploadException, IOException, ServletException {
downloadFile(sourceFilePathName, contentType, null);
}
/**
* Downloads a file.
* @param sourceFilePathName java.lang.String The full path to the file to download.
* @param contentType java.lang.String the content type.
* @param destFileName java.lang.String the suggested file name.
* @exception UploadException
* @exception IOException
* @exception ServletException
*/
public void downloadFile(
String sourceFilePathName,
String contentType,
String destFileName)
throws UploadException, IOException, ServletException {
downloadFile(sourceFilePathName, contentType, destFileName, 65000);
}
/**
* Downloads a file.
* @param sourceFilePathName java.lang.String The full path to the file to download.
* @param contentType java.lang.String the content type.
* @param destFileName java.lang.String the suggested file name.
* @param blockSize int block size
*/
public void downloadFile(
String sourceFilePathName,
String contentType,
String destFileName,
int blockSize)
throws UploadException, IOException, ServletException {
if (sourceFilePathName == null)
throw new IllegalArgumentException(
String.valueOf(
(new StringBuffer("File '")).append(sourceFilePathName).append(
"' not found (1040).")));
if (sourceFilePathName.equals(""))
throw new IllegalArgumentException(
String.valueOf(
(new StringBuffer("File '")).append(sourceFilePathName).append(
"' not found (1040).")));
if (!isVirtual(sourceFilePathName) && m_denyPhysicalPath)
throw new SecurityException("Physical path is denied (1035).");
if (isVirtual(sourceFilePathName))
sourceFilePathName = m_application.getRealPath(sourceFilePathName);
java.io.File file = new java.io.File(sourceFilePathName);
FileInputStream fileIn = new FileInputStream(file);
long fileLen = file.length();
int readBytes = 0;
int totalRead = 0;
byte b[] = new byte[blockSize];
if (contentType == null)
m_response.setContentType("application/x-filler");
else if (contentType.length() == 0)
m_response.setContentType("application/x-filler");
else
m_response.setContentType(contentType);
m_response.setContentLength((int) fileLen);
m_contentDisposition =
m_contentDisposition != null ? m_contentDisposition : "attachment;";
if (destFileName == null)
m_response.setHeader(
"Content-Disposition",
String.valueOf(
(new StringBuffer(String.valueOf(m_contentDisposition)))
.append(" filename=")
.append(getFileName(sourceFilePathName))));
else if (destFileName.length() == 0)
m_response.setHeader("Content-Disposition", m_contentDisposition);
else
m_response.setHeader(
"Content-Disposition",
String.valueOf(
(new StringBuffer(String.valueOf(m_contentDisposition)))
.append(" filename=")
.append(destFileName)));
while ((long) totalRead
import java.io.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
/**
* The object which perfoms the download.
* @author: Leon Zhao
*/
public class Download {
protected ServletContext m_application;
protected HttpServletRequest m_request;
protected HttpServletResponse m_response;
private boolean m_denyPhysicalPath;
private boolean m_forcePhysicalPath;
private String m_contentDisposition;
private Vector m_deniedFilesList;
private Vector m_allowedFilesList;
/**
* save in a virtual if it exists else consider the path as a physical if the porperty denyPhysicalPath is false.
*/
public static final int SAVE_AUTO = 0;
/**
* save the file only if the path is a virtual path.
*/
public static final int SAVE_VIRTUAL = 1;
/**
* save the file only if the path is a physical path.
*/
public static final int SAVE_PHYSICAL = 2;
/**
* open the file only if the path is a physical path.
*/
/**
* Download constructor comment.
*/
public Download() {
m_deniedFilesList = new Vector();
m_allowedFilesList = new Vector();
m_denyPhysicalPath = false;
m_forcePhysicalPath = false;
m_contentDisposition = new String();
}
/**
* Downloads a file.
* @param sourceFilePathName java.lang.String The full path to the file to download.
* @exception UploadException
* @exception IOException
* @exception ServletException
*/
public void downloadFile(String sourceFilePathName)
throws UploadException, IOException, ServletException {
downloadFile(sourceFilePathName, null, null);
}
/**
* Downloads a file.
* @param sourceFilePathName java.lang.String The full path to the file to download.
* @param contentType java.lang.String the content type.
* @exception UploadException
* @exception IOException
* @exception ServletException
*/
public void downloadFile(String sourceFilePathName, String contentType)
throws UploadException, IOException, ServletException {
downloadFile(sourceFilePathName, contentType, null);
}
/**
* Downloads a file.
* @param sourceFilePathName java.lang.String The full path to the file to download.
* @param contentType java.lang.String the content type.
* @param destFileName java.lang.String the suggested file name.
* @exception UploadException
* @exception IOException
* @exception ServletException
*/
public void downloadFile(
String sourceFilePathName,
String contentType,
String destFileName)
throws UploadException, IOException, ServletException {
downloadFile(sourceFilePathName, contentType, destFileName, 65000);
}
/**
* Downloads a file.
* @param sourceFilePathName java.lang.String The full path to the file to download.
* @param contentType java.lang.String the content type.
* @param destFileName java.lang.String the suggested file name.
* @param blockSize int block size
*/
public void downloadFile(
String sourceFilePathName,
String contentType,
String destFileName,
int blockSize)
throws UploadException, IOException, ServletException {
if (sourceFilePathName == null)
throw new IllegalArgumentException(
String.valueOf(
(new StringBuffer("File '")).append(sourceFilePathName).append(
"' not found (1040).")));
if (sourceFilePathName.equals(""))
throw new IllegalArgumentException(
String.valueOf(
(new StringBuffer("File '")).append(sourceFilePathName).append(
"' not found (1040).")));
if (!isVirtual(sourceFilePathName) && m_denyPhysicalPath)
throw new SecurityException("Physical path is denied (1035).");
if (isVirtual(sourceFilePathName))
sourceFilePathName = m_application.getRealPath(sourceFilePathName);
java.io.File file = new java.io.File(sourceFilePathName);
FileInputStream fileIn = new FileInputStream(file);
long fileLen = file.length();
int readBytes = 0;
int totalRead = 0;
byte b[] = new byte[blockSize];
if (contentType == null)
m_response.setContentType("application/x-filler");
else if (contentType.length() == 0)
m_response.setContentType("application/x-filler");
else
m_response.setContentType(contentType);
m_response.setContentLength((int) fileLen);
m_contentDisposition =
m_contentDisposition != null ? m_contentDisposition : "attachment;";
if (destFileName == null)
m_response.setHeader(
"Content-Disposition",
String.valueOf(
(new StringBuffer(String.valueOf(m_contentDisposition)))
.append(" filename=")
.append(getFileName(sourceFilePathName))));
else if (destFileName.length() == 0)
m_response.setHeader("Content-Disposition", m_contentDisposition);
else
m_response.setHeader(
"Content-Disposition",
String.valueOf(
(new StringBuffer(String.valueOf(m_contentDisposition)))
.append(" filename=")
.append(destFileName)));
while ((long) totalRead