2009년 7월 1일 수요일

filedown

package com.java.fileup;

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.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 public class Download extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  execute(request, response);
}  
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
execute(request, response);
}        
//사용자정의 함수---------------------------------------------------------------------------
protected void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = getServletContext().getRealPath("/storage");  //서비스항목으로 위치 찾기
String filename = new String(request.getParameter("clip").getBytes("8859_1"),"euckr");
String originalfilename = new String(request.getParameter("original").getBytes("8859_1"),"euckr");
File file = new File(path + "/" + filename);
System.out.println("파일경로 : " + file);
setHeaderType(response, file,originalfilename);
transport(new FileInputStream(file), response.getOutputStream(), file);
}
//파일의 이름, 용량, 파일의 타입(이미지,음악,텍스트....) : 사용자정의함수----------------
private void setHeaderType(HttpServletResponse response, File file,String originalfilename)
{
String mime = getServletContext().getMimeType(file.toString());
if(mime != null)mime = "application/octet-stream";
response.setContentType(mime);
response.setHeader("Content-Disposition",
                         "attachment;filename=" + K2E(replace(originalfilename)));
response.setHeader("Content-Length", ""+file.length());
}
//파일전송 : 사용자정의함수----------------------------------------------------------------
private void transport(InputStream in, OutputStream out, File file)
throws IOException
{
BufferedInputStream  bin = null;
BufferedOutputStream bos = null;
try{
bin = new BufferedInputStream(in);
bos = new BufferedOutputStream(out);
byte [] buf = new byte[(int) file.length()];
int read=0;
while((read = bin.read(buf))  != -1)
{
bos.write(buf, 0, read);                //객체, 시작(offset),길이
}
}catch(Exception e){
System.out.println("transport error : " + e);
}finally{
bos.close();
bin.close();
}
}
 ///////////////////////////////////////////////////////////////////////
public String K2E(String  str)
{
String tmp = null;
try{
tmp = new String (str.getBytes("euckr"), "8859_1");
}catch(Exception e){ }
return tmp;
}
 
////파일이름에 .이 있는 경우 [] 붙는거 방지////////////////////////////
// subject는 파일이름
// replace() 파일을 받을시 '.'이 [].으로 변경을 방지..

public String replace(String subject)
{
String replaced="";
int index=0;
StringBuffer st=new StringBuffer();

index=subject.lastIndexOf(".");
replaced=new String(subject.substring(0,index));

//           '.'    =>    UTF-8 code    =>   '%2e'
replaced=replaced.replaceAll("\\.","%2e");
st.append(replaced);
st.append(subject.subSequence(index,subject.length()));
return st.toString();
}
 }
 

댓글 없음:

댓글 쓰기