fileDown
博客专区 > 小白N 的博客 > 博客详情
fileDown
小白N 发表于10个月前
fileDown
  • 发表于 10个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

package com.adobe.sunTrust; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Property; import org.apache.felix.scr.annotations.Service; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory;

@Component @Service @Property( name="scheduler.period", longValue = 10) public class FileDown implements Runnable { private Logger logger = LoggerFactory.getLogger(this.getClass());

private BundleContext bundleContext;

public void run() {
	logger.info("Running...");
}
protected void activate(ComponentContext ctx) {
	this.bundleContext = ctx.getBundleContext();
}
protected void deactivate(ComponentContext ctx) {
	this.bundleContext = null;
}

public void fileDown(String path,HttpServletResponse response){
	try {			
	    File file = new File(path);
	    String filename = file.getName();
	    filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
	    InputStream fis = new BufferedInputStream(new FileInputStream(path));
	    byte[] buffer = new byte[fis.available()];
	    fis.read(buffer);
	    fis.close();
	    response.reset();
	    response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
	    response.addHeader("Content-Length", "" + file.length());
	    OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
	    response.setContentType("application/octet-stream");
	    toClient.write(buffer);
	    toClient.flush();
	    toClient.close();
	    		    		    				    
	} catch (IOException ex) {
	     ex.printStackTrace();
	}	
}

}

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 2
码字总数 358
×
小白N
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: