2013년 8월 11일 일요일

스트럿츠 멀티 게시판2. 기본 설정( 2 ) - 김길재

이번 글에 나오는 모든 파일 역시

WEB-INF/src/multiboard 밑에 저장합니다. 


  오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의)  

파일 업로드경로에 쓰이는 전역 변수를 선언하는 부분입니다.

/////////////// Constants.java /////////////////////////////////////

package multiboard;


public class Constants
{
        private String FILE_PATH = "D:/workspace/multiboard/board/upload/";
       

        /**
        * @return Returns the fILE_PATH.
        */
        public String getFILE_PATH() {
                return FILE_PATH;
        }
       
}

파일 업로드에 쓰이는 파일입니다.


/////////////////////// FileUploadUtil.java //////////////////////////

package multiboard;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts.upload.FormFile;


public class FileUploadUtil
{
        public static String FileUpload( FormFile file , String file_name )
        throws FileNotFoundException, IOException
        {
                try
                {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        InputStream stream = file.getInputStream();

                        KKJLog.info( "///////////////////////////////////////////////////////" );
                        KKJLog.info( "FileUploadUtil info " + file_name + "'s uploading start" );
                        OutputStream bos = new FileOutputStream( file_name );
                        int bytesRead = 0;
                        byte[] buffer = new byte[8192];
                        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
                        {
                                bos.write(buffer, 0, bytesRead);
                        }
                        bos.close();
                        stream.close();
                        KKJLog.info( "FileUploadUtil info " + file_name + "'s uploading finish" );
                        KKJLog.info( "///////////////////////////////////////////////////////" );
                        KKJLog.info( "FileUploadUtil info " + file_name + " is uploaded" );
                        KKJLog.info( "///////////////////////////////////////////////////////" );
                }
                catch( Exception e )
                {
                        KKJLog.info( "/////////////////////////////////////////////" );
                        KKJLog.info( "FileUploadUtil Error : " + e.toString() );
                        KKJLog.info( "/////////////////////////////////////////////" );
                        return null;
                }
                return "true";
        }
}

다음으로 공통 메서드를 모아 놓은 Util.java입니다.

//////////////////// Util.java //////////////////////////////
package multiboard;

import java.io.UnsupportedEncodingException;
import org.apache.struts.upload.FormFile;

public class Util {
   
    public static String ksc2uni(String str) throws UnsupportedEncodingException {
        if(str==null) return null;
        return new String(str.getBytes("KSC5601"),"8859_1");
    }
   
    public static String uni2ksc(String str) throws UnsupportedEncodingException {
        if(str==null) return null;
        return new String(str.getBytes("8859_1"),"KSC5601");
        }
   
    public static String lineBreak(String src)
    {
        int len = src.length();
        int linenum = 0, i = 0;

        for (i = 0; i < len; i++)
          if (src.charAt(i) == '\n')
            linenum++;

        StringBuffer dest = new StringBuffer(len + linenum * 3);

        for (i = 0; i < len; i++) {
          if (src.charAt(i) == '\n')
            dest.append("<br>");
          else
            dest.append(src.charAt(i));
        }

        return dest.toString();
    }
   
    public static String getExtension( FormFile file )
    {
            return file.getFileName().substring( file.getFileName().lastIndexOf( '.' ) );
    }
}

댓글 없음:

댓글 쓰기