请问哪位大神有iteye账号分享,求帮忙下载,不胜感激~~~麻烦了

博客分类:
Http文件下载的普通模式就不多说了,断点下载与普通模式不一样的是,断点下载的头信息里面增加了一个属性RANGE: bytes=100000-这里RANGE代表客户端要从那个位置开始下载
而服务器返回时和普通模式不同的是:1:多了一个属性Content-Range=bytes 99/20000注意还有一些属性要设置,和普通模式一样,例如Content-Length属性2:返回码为206
然后我们来看一段实际应用中的断点下载代码,注意这里我使用的是SpringMVC模式开发的:
@SuppressWarnings({ "unchecked" })
@RequestMapping(value = "/downOdex.do")
public ResponseEntity&String& downFile(
@RequestParam(value="odexName")String odexName,
HttpServletResponse response,
HttpServletRequest request){
InputStream inputStream =
ServletOutputStream out =
File file = new File(OdexManage.odexFileBasePath + "\\" + odexName);
int fSize = Integer.parseInt(String.valueOf(file.length()));
response.setCharacterEncoding("utf-8");
response.setContentType("application/x-download");
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Content-Length", String.valueOf(fSize));
response.setHeader("Content-Disposition", "fileName=" + odexName);
inputStream=new FileInputStream(OdexManage.odexFileBasePath + "\\" + odexName);
long pos = 0;
if (null != request.getHeader("Range")) {
// 断点续传
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
pos = Long.parseLong(request.getHeader("Range").replaceAll("bytes=", "").replaceAll("-", ""));
} catch (NumberFormatException e) {
out = response.getOutputStream();
String contentRange = new StringBuffer("bytes ").append(pos+"").append("-").append((fSize - 1)+"").append("/").append(fSize+"").toString();
response.setHeader("Content-Range", contentRange);
inputStream.skip(pos);
byte[] buffer = new byte[1024*10];
int length = 0;
while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, length);
Thread.sleep(100);
} catch (Exception e) {
logger.error("ODEX软件下载异常:"+e);
if(null != out) out.flush();
if(null != out) out.close();
if(null != inputStream) inputStream.close();
} catch (IOException e) {
return new ResponseEntity(null,HttpStatus.OK);
其重点在于HTTP协议里面属性有一些不同的地方,还有就是InputStream跳过不需要读的文件,和注意关闭流。通过核心代码也可以看到,其实这和是不是SpringMVC没多大关系,所以你可以很容易的应用到你的项目中。
例如对于这段代码,我访问如下路径
使用浏览器自带的下载工具进行下载,这样可以控制下载和暂停,来观察断点下载的过程。
点击暂停会看到下载暂停下来
可以选择继续下载。
过程中你会看到,客户端不会马上把文件下载下来,而是慢慢下载,就是因为我在程序中增加了停顿来查看这个下载过程
Thread.sleep(100);
然后我们的缓冲区是
byte[] buffer = new byte[1024*10];
所以,每秒的速度就是100KB理论速度,因为是本地,所以接近100KB每秒。
您到ITEYE网站看 java小强 原创,谢谢!!
自建博客地址: ,内容与ITEYE同步!
浏览 22186
response.setHeader("Content-Range", contentRange);&&& 有个疑问,你这是不是只能一次断点下载?没明白你的意思?
cuisuqiang
浏览: 3223283 次
来自: 北京
浏览量:3001100
你这应该只使用于windows系统吧?linux系统怎么转换? ...
貌似少了一个java文件哈 ...
cuisuqiang 写道jlcon 写道chenqidou
这个字段用法是如果相互之类超过多少时间没有数据交互,才抛出的正 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'清除回答草稿
&&&您需要以后才能回答,未注册用户请先。博客分类:
维护一个旧项目(eJMS),先前从JDK1.3升级到1.5,后来还要从FTP转换到SFTP
转SFTP用了一个开源的jftp.jar包支持,download的代码
public byte[] downloadFile(String remoteDir, String fileName){
JSch jsch = new JSch();
session = jsch.getSession(this.userName, this.hostName, this.port);
System.out.println("Get Session : " + session);
session.setPassword(password);
System.out.println("set password ... ");
session.setUserInfo(defaultUserInfo);
System.out.println("set user info ... ");
session.connect();
System.out.println("connected session : sftp://" + this.hostName + ":" + this.port);
channel = session.openChannel("sftp");
System.out.println("opened channel ... ");
channel.connect();
System.out.println("connected channel ... ");
ChannelSftp c = (ChannelSftp)
System.out.println("remoted channel ... ");
c.cd(remoteDir);
Vector getFile = new Vector();
InputStream in = c.get(fileName);
int length = 0;
int totalLength = 0;
byte[] buffer = new byte[1024];
while ((length = in.read(buffer)) & 0){
byte[] tmpBuffer = new byte[length];
System.arraycopy(buffer, 0, tmpBuffer, 0, length);
getFile.addElement(tmpBuffer);
totalLength = totalLength +
in.close();
byte[] result = new byte[totalLength];
int pos = 0;
for (int i = 0; i & getFile.size(); i ++){
byte[] tmpBuffer = (byte[])getFile.elementAt(i);
System.arraycopy(tmpBuffer, 0, result, pos, tmpBuffer.length);
pos = pos + tmpBuffer.
getFile.clear();
System.out.println("downloaded file '" + remoteDir + "\\" + fileName + "'");
c.disconnect();
System.out.println("disconnected channel ... ");
session.disconnect();
System.out.println("disconnected session ... ");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
调用download的方法
public byte[] getFile(String server, String user, String pwd, String tar_dir, String filename) {
if(server!=null && user != null && pwd!=null) {
SFtp sftp = new SFtp(server, user, pwd);
return sftp.downloadFile(tar_dir, filename);
上传的代码
public boolean uploadFile(String remoteDir, String fileName, byte[] data){
JSch jsch = new JSch();
session = jsch.getSession(this.userName, this.hostName, this.port);
System.out.println("Get Session : " + session);
session.setPassword(password);
System.out.println("set password ... ");
session.setUserInfo(defaultUserInfo);
System.out.println("set user info ... ");
session.connect();
System.out.println("connected session : sftp://" + this.hostName + ":" + this.port);
channel = session.openChannel("sftp");
System.out.println("opened channel ... ");
channel.connect();
System.out.println("connected channel ... ");
ChannelSftp c = (ChannelSftp)
System.out.println("remoted channel ... ");
c.cd(remoteDir);
OutputStream out = c.put(fileName);
int length = data.
out.write(data, 0, length);
out.flush();
out.close();
System.out.println("uploaded file to '" + remoteDir + "\\" + fileName + "'");
c.disconnect();
System.out.println("disconnected channel ... ");
session.disconnect();
System.out.println("disconnected session ... ");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
调用上传的方法的部分代码
public int getUploadFile(HttpServletRequest request, int maxSize){
maxsize = maxS
int length=0;
// Setup incoming data
multi = new MultipartRequest(request, );
int flength = multi.checkFileSize();
// check file's length
if(flength & (maxSize * 1024)){
return -1;
multi.getFileData();
Hashtable all=multi.getAllFile();
Enumeration files = all.elements();
while(files.hasMoreElements()) {
UploadedFile up = (UploadedFile)files.nextElement();
// upload file must be *.doc
if (!up.getFilesystemName().toLowerCase().endsWith(".doc")){
return -2;
byte[] data = up.getData();
log(server+", "+user+","+pwd+","+tar_dir+","+filename);
SFtp sftp = new SFtp(server, portNo, user, pwd);
if (sftp.uploadFile(tar_dir, filename, data)
length = length + data.
log("ftp file error:" + filename);
} catch (Exception e) {
log(e, "upload error");
上传的时候直接call JavaBean就可以了
下载的时候由于返回的是byte[],所以call JavaBean之后还需要返回给客户端
ServletOutputStream outs = response.getOutputStream();
outs.write(data);
outs.flush();
outs.close();
这里还要注意一个问题,当你引入一些package或者其他定义之类的代码,它们之间不可以存在有空格
比如这样没有问题
&%%&&%%&&%
但是如果是
就会抛出异常
15:17:01 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
这个问题我是不怎么好理解...
描述: 开源包
下载次数: 320
浏览: 176973 次
来自: 广州
mythal 写道不好意思,StringUtils.split ...
受用,支持!
for(File attach:attachments){
liupeng_10408 写道博主,你好!我开发的是andr ...
博主,你好!我开发的是android版邮件系统。
只写了如下几 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'谁有ITEYE的账号 ,帮我下一个文档,新用户要1天之后才能下载 急求_百度知道
谁有ITEYE的账号 ,帮我下一个文档,新用户要1天之后才能下载 急求
网址:感谢
我有更好的答案
&跟开涛学 SpringMVC.pdf&,SpringMVC核心配置文件示例.rar,&Spring MVC 教程,快速入门,深入.pdf&搞定还望及时采纳
跟着开涛学SpringMVC源码:
采纳率:69%
来自团队:
为您推荐:
其他类似问题
阿维的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。博客分类:
1.求最小子序列的和
就是对于连续的序列,找出连续序列中和最小的
例如:int a[LEN] = {4,-1,5,-2,-1,2,6,-2,1,-3};
最小的子序列就是:-2,1,-3
对于下面的最大子序列就是:4,-1,5,-2,-1,2,6。
*最小子序列和
int subMinSum(int a[], int length){
int thismin = 0, min = INT_MAX;
for(int i=0; i& i++){
thismin += a[i];
if(thismin & min){
}else if(thismin & 0){
thismin = 0;
2.最大子序列的和,其思想和求最小是一样的
*最大子序列和
int subMaxSum(int *a, int length){
int thismax = a[0], max=INT_MIN;//注:此处修正了thismax = 0,因为全负数的时候返回0,这里初值设置为a[0]那么会返回最大负数
for(int i=0; i& i++){
thismax += a[i];
if(thismax & max){
}else if(thismax & 0){
thismax = 0;
针对上面的两种算法,效率是比较高的,但是正确性不容易看出来,需要自己仔细的揣摩和证明,但是优点也很明显,就是对数据只需要一次的扫描,顺序读入,这对于大量数据来说是很有利的,只要全部读入数据就可以得出结果,这个在算法分析一书中叫做“联机算法(on-line algorithm)”,意思就是上面描述的,线性时间完成的联机算法基本上算法完美算法。
3.求最大子序列,并记录子序列位置
这里我们肯定很容易记录下最终的位置,就是不容易确定序列的起始位置,主要是由于该算法的特性,最后想了好久才想到用:减最大的值的方法,当等于0的时候就找到起始位置了,从而确定序列范围
#include &iostream&
int maxsub(const int* a, int length, int* loc){
//maxsum记录最大的值,thissum记录当前的值
int maxsum=0, thissum=0, i=0;
if(length &= 0) return 0;
for(i=0; i& i++){
thissum += a[i];
if(maxsum & thissum){
//这里有一个思想就是,为负数的子序列不可能成为最优子序列的前缀,
}else if(thissum & 0){
thissum = 0;
for(i=loc[1]; i&=0; i--){
thissum -= a[i];
if(thissum == 0){
int main(){
int a[] = {2, -3, 7, -4,-2,-2,6,-2};
int loc[2]={0};
cout&&maxsub(a, 8, loc)&&
cout&&"from:"&&loc[0]&&"---to:"&&loc[1]&&
4.最小正子序列和
这个是在一博客中看到的:
然后我有一个疑问就是,究竟什么是最小正子序列和?这个在网上找了也没有个好的答案(),如果哪位仁兄知道,不胜感激!!
对于下面求出的sum我也没搞的太清楚是为什么?然后又循环什么的?
struct Node
int cmp(const Node& t1,const Node& t2)
return t1.sum & t2.
*最小正子序列和
int positiveSubMinSum(int *data, int len){
Node* temp = new Node[len];
temp[0].sum = data[0];
temp[0].xiabiao = 0;
for(int i=1;i&i++)
temp[i].sum = temp[i-1].sum+data[i];
temp[i].xiabiao =
//对temp.sum[]进行从小到大排序,sum[]中只有相邻的两个数才有可能 得到 最小正子序列和
sort(temp,temp+len,cmp);
int sum = INT_MAX;
for(int i=0;i&len-1;i++)
if(temp[i].xiabiao & temp[i+1].xiabiao)
if(temp[i+1].sum - temp[i].sum & 0 && temp[i+1].sum - temp[i].sum & sum)
sum = temp[i+1].sum - temp[i].
5.最大子序列的乘积
对网上的一些算法进行了下比较,发现这个算法还可以,比较简洁:
对该算法的说明:
这个问题其实可以简化成这样:数组中找一个子序列,使得它的乘积最大;同时找一个
子序列,使得它的乘积最小(负数的情况)。虽然我们只要一个最大积,但由于负数的
存在,我们同时找这两个乘积做起来反而方便。
我们让maxCurrent表示当前最大乘积的candidate,minCurrent反之,表示当前最小乘积
的candidate。这里我用candidate这个词是因为只是可能成为新一轮的最大/最小乘积,
而maxProduct则记录到目前为止所有最大乘积candidates的最大值。
由于空集的乘积定义为1,在搜索数组前,maxCurrent,maxProduct,minCurrent都赋为1。
假设在任何时刻你已经有了maxCurrent和minCurrent这两个最大/最小乘积的candidates,
新读入数组的元素x(i)后,新的最大乘积candidate只可能是maxCurrent或者minCurrent
与x(i)的乘积中的较大者,如果x(i)&0导致maxCurrent&minCurrent,需要交换这两个
candidates的值。
当任何时候maxCurrent&1,由于1(空集)是比maxCurrent更好的candidate,所以更新
maxCurrent为1,类似的可以更新minCurrent。任何时候maxCurrent如果比最好的
maxProduct大,更新maxProduct。
void swap(int& a, int& b){
int temp =
*最大子序列乘积(同时也求出了最小的子序列乘积)
*在找最大的值得时候,必须记录最小值,因为有负数存在,最小的数可能变成最大的数
int mutiSubMax(int *a, int length){
int maxProduct = 1;
int minProduct = 1;
int maxCurrent = 1;
int minCurrent = 1;
for( i=0; i&i++)
maxCurrent *= a[i];
minCurrent *= a[i];
if(maxCurrent & maxProduct)
maxProduct = maxC
if(minCurrent & maxProduct)
maxProduct = minC
if(maxCurrent & minProduct)
minProduct = maxC
if(minCurrent & minProduct)
minProduct = minC
//注意交换
if(minCurrent & maxCurrent)
swap(maxCurrent,minCurrent);
//这个必须在最后(防止为0的时候)
if(maxCurrent&1)
maxCurrent = 1;
//if(minCurrent&1)//这里不需要,因为通过交换即可,只需要一个
minCurrent =1;
return maxP
6.最长递增子序列参考:
a,问题描述
设L=&a1,a2,…,an&是n个不同的实数的序列,L的递增子序列是这样一个子序列Lin=&aK1,ak2,…,akm&,其中k1&k2&…&km且aK1&ak2&…&akm。求最大的m值
b,问题分析
最长递增子序列可以看成一个动态规划的问题,关于动态规划可以参见:
其实质就是:将问题细化,逐步求解
当然实际的过程相对复杂些,就拿该题来说
如:子序列{1, 9, 3, 8, 11, 4, 5, 6, 4, 19, 7, 1, 7 }这样一个字符串的的最长递增子序列就是{1,3,4,5,6,7}或者{1,3,4,5,6,19}。
首先,该问题可以抽象为:子序列为L(1..n),
A(1...i)是一个从1到i的优化的子结构,也就是最长递增子序列,求A(n)。
其次,根据上面的抽象,求的就是A(n), 那么问题就转化为求A(j)与A(1...i)(j&i)的关系(状态转移方程)。
最后,根据动态规划的概念,找的就是上面的这样一个关系,如何将A(j)与A(1...i)联系起来?从而将问题细化,L(j) = max {L(i), i&j && A(i)&A(j) } + 1;
也就是说L(j)等于之前所有的L(i)中最大的的L(i)加一。这样的L(i)需要满足的条件就是A(i)&A(j).这个推断还是比较容易理解的.就是选择j之前所有的满足小于当前数组的最大值.
然后就是将上面的关系式转换为代码,这个就很简单了。
过程如下:
*最长递增子序列
#include &iostream&
#include &cstring&
//利用动态规划法O(n^2)
void longestIncreaseSub(int* a, int length, int* sub){
//利用一个辅助数列,记录子问题的值
int *t = new int[n];//需要将t所有都初始化1,t记录子序列(子问题)的最长递增子序列长度
int *p = new int[n];
memset(p,0,sizeof(int));
//初始的最大子序列长度默认为1,即自身组成一个最长递增子序列
for(int i=1; i&n; i++){
for(int j=0; j&i; j++){
//这里面就是动态优化的状态转移方程
if(a[j]&a[i] && t[j]&t[i]-1){
//里面存的是(0到i)的子序列中最长递增子序列的长度
t[i]=t[j]+1;
//符合要求的子序列位置(就是上一个子序列中的最后一个值的位置)
int m=0,k=0;
for (int i=1;i&=n;i++)
if (m&t[i])
{//m存t中最大值(就是要找的最长递增子序列),i是其所在的索引
while(m&=0){
sub[m-1] = a[k];
//p[k]中存着子序列中下一个值的下标位置
for(int d=0; d&n; d++) cout&&t[d]&&" ";
for(int d=0; d&n; d++) cout&&p[d]&&" ";
int main(){
int a[] = { 1, 9, 3, 8, 11, 4, 5, 6, 4, 19, 7, 1, 7 };
int length = sizeof(a)/sizeof(int);
int* sub = new int[length];
memset(sub,0,sizeof(int));//初始化sub
longestIncreaseSub(a,length, sub);
for(int k=0; k& k++) cout&&sub[k]&&" ";
sub = NULL;
7.上面的1,2,4,5的代码全部
*求最小子序列和,最小正子序列和,最大子序列乘积
#include &iostream&
#include &algorithm&
#define INT_MIN -32768
#define INT_MAX 32767
//在里面有使用#define和const定义常量的比较,推荐使用const
//#define LEN 10
const int LEN = 10;
*最大子序列和
int subMaxSum(int *a, int length){
int thismax = 0, max=INT_MIN;
for(int i=0; i& i++){
thismax += a[i];
if(thismax & max){
}else if(thismax & 0){
thismax = 0;
*最小子序列和
int subMinSum(int a[], int length){
int thismin = 0, min = INT_MAX;
for(int i=0; i& i++){
thismin += a[i];
if(thismin & min){
}else if(thismin & 0){
thismin = 0;
struct Node
int cmp(const Node& t1,const Node& t2)
return t1.sum & t2.
*最小正子序列和
int positiveSubMinSum(int *data, int len){
Node* temp = new Node[len];
temp[0].sum = data[0];
temp[0].xiabiao = 0;
for(int i=1;i&i++)
temp[i].sum = temp[i-1].sum+data[i];
temp[i].xiabiao =
//对temp.sum[]进行从小到大排序,sum[]中只有相邻的两个数才有可能 得到 最小正子序列和
sort(temp,temp+len,cmp);
int sum = INT_MAX;
for(int i=0;i&len-1;i++)
if(temp[i].xiabiao & temp[i+1].xiabiao)
if(temp[i+1].sum - temp[i].sum & 0 && temp[i+1].sum - temp[i].sum & sum)
sum = temp[i+1].sum - temp[i].
void swap(int& a, int& b){
int temp =
*最大子序列乘积(同时也求出了最小的子序列乘积)
*在找最大的值得时候,必须记录最小值,因为有负数存在,最小的数可能变成最大的数
int mutiSubMax(int *a, int length){
int maxProduct = 1;
int minProduct = 1;
int maxCurrent = 1;
int minCurrent = 1;
for( i=0; i&i++)
maxCurrent *= a[i];
minCurrent *= a[i];
if(maxCurrent & maxProduct)
maxProduct = maxC
if(minCurrent & maxProduct)
maxProduct = minC
if(maxCurrent & minProduct)
minProduct = maxC
if(minCurrent & minProduct)
minProduct = minC
//注意交换
if(minCurrent & maxCurrent)
swap(maxCurrent,minCurrent);
//这个必须在最后(防止为0的时候)
if(maxCurrent&1)
maxCurrent = 1;
//if(minCurrent&1)//这里不需要,因为通过交换即可,只需要一个
minCurrent =1;
return maxP
int main(){
int a[LEN] = {4,-1,5,-2,-1,2,6,-2,1,-3};
cout&&"列表:"&&
for(int i=0; i&10; i++){
cout&&a[i]&&" ";
cout&&"最大子序列和:"&&subMaxSum(a, LEN)&&
cout&&"最小子序列和:"&&subMinSum(a, LEN)&&
cout&&"最小正子序列和:"&&positiveSubMinSum(a, LEN)&&
cout&&"最大子序列乘积:"&&mutiSubMax(a, LEN)&&
hao3100590
浏览: 96953 次
来自: 成都
你好,多谢分享,问个问题,在上传数据的时候判断文件是否有上传记 ...
为什么我的tabhost显示不出来? 怎么设置在全部页面中让他 ...
大神篇,思路,配图都很清晰,perfect!
牛死人了!!!
很牛的文档。数学功底好啊
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 iteye账号分享 的文章

 

随机推荐