⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadservicebean.java

📁 基于Spring2.5的精品课程网站
💻 JAVA
字号:
package org.adam.service.impl;

import org.adam.bean.UploadBean;
import org.adam.service.DownloadService;
import org.adam.service.UploadService;
import org.adam.tools.Factory;

import java.io.UnsupportedEncodingException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class DownloadServiceBean implements DownloadService{

	private String filename;
	private String path;
	
	private int downloadresult;
	
	public String download(String name) {		
		return name;
	}

	public int download() {
		System.out.println("begin to download");
		return downloadresult;
	}

	public void down(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		request.setCharacterEncoding("GBK");
		HttpSession session=request.getSession();
		int upload_id=Integer.parseInt(new String(session.getAttribute("upload_id").toString()));
		UploadService uploadService=(UploadService)Factory.getBeanName("uploadService");
		UploadBean upload=uploadService.getDownloadResource(upload_id);
		filename=upload.getUploadname();
		path="D:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/GoodSubject/fileupload"+"/"+filename;
		
	}
	
	public void download(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("GBK");
		HttpSession session=request.getSession();
		int upload_id=Integer.parseInt(new String(session.getAttribute("upload_id").toString()));
		UploadService uploadService=(UploadService)Factory.getBean("uploadService");
		UploadBean upload=uploadService.getDownloadResource(upload_id);
		filename=upload.getUploadname();
		path = "D:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/GoodSubject/fileupload"+"/"+filename;
		downfile(response);
	}
	
	protected void write(OutputStream os) throws IOException {
		FileInputStream fis = new FileInputStream(path);
		BufferedInputStream bis = new BufferedInputStream(fis);
		byte[] s = new byte[1024];
		int i = 0;
		while ((i = bis.read(s)) > 0) {
			os.write(s, 0, i);
		}
		if (os != null) {
			os.flush();
			os.close();
		}
		if (bis != null)
			bis.close();
		if (fis != null)
			fis.close();
	}
	
	public static String toUTF8String(String s){
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<s.length();i++){
			char c=s.charAt(i);
			if(c>=0&&c<=255){
				sb.append(c);
			}
			else{
				byte[] b;
				try{
					b=Character.toString(c).getBytes("UTF-8");
				}catch(Exception e){System.out.println(e);b=new byte[0];}
				for(int j=0;j<b.length;j++){
					int k=b[j];
					if(k<0)
						k+=256;
					sb.append("%"+Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}
	
	public void downfile(HttpServletResponse response) throws IOException{
		String f;
		if(filename!=null)
		{
			File file=new File(path);
			
			if(!file.exists()) throw new IOException("文件不存在:"+file.getPath());
			else{
				f=toUTF8String(filename);
				
				response.setContentType("application/x-msdownload");
				response.setContentLength((int)file.length());
				response.setHeader("Content-Disposition", "attachment;filename="+f);
				write(response.getOutputStream());
			}
		}
	}
	
	public void nopower(HttpServletResponse response)throws IOException{
		response.sendRedirect("poweroff.jsp");
	}

	public int getDownloadresult() {
		return downloadresult;
	}

	public void setDownloadresult(int downloadresult) {
		this.downloadresult = downloadresult;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -