[java]FilePart(fileUpload)

2014. 2. 12. 17:28ยทLanguages/JAVA
package com.healthcare.common.upload.http;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletInputStream;

public class FilePart extends Part {

	private String fileName;
	private String filePath;
	private String contentType;
	private PartInputStream partInput;

	FilePart(String name, ServletInputStream in, String boundary,
	String contentType, String fileName, String filePath)
	throws IOException {
		super(name);
		this.fileName = fileName;
		this.filePath = filePath;
		this.contentType = contentType;
		partInput = new PartInputStream(in, boundary);
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFilePath() {
		return filePath;
	}

	public String getContentType() {
		return contentType;
	}

	public InputStream getInputStream() {
		return partInput;
	}

	public UploadedInfo writeTo(File fileOrDirectory,String locale) throws IOException {
		long written = 0;
		OutputStream fileOut = null;
		UploadedInfo info = new UploadedInfo();
		try {
			if (fileName != null) {
				info.original = asc2euc(fileName,locale);
				File file;
				if (fileOrDirectory.isDirectory()) {
					fileName = checkAndGetFileName(fileOrDirectory, asc2euc(fileName,locale));
					info.renames = fileName;
					file = new File(fileOrDirectory, fileName);
				} else {
					file = fileOrDirectory;
				}
				fileOut = new BufferedOutputStream(new FileOutputStream(file));
				written = write(fileOut);
				info.size = written;
			}
		} finally {
			if (fileOut != null) fileOut.close();
		}

		return info;
	}

//	public com.innosns.util.ByteArrayDataSource getDataSource() throws IOException{
//		 com.innosns.util.ByteArrayDataSource dataSource = new com.innosns.util.ByteArrayDataSource(partInput,contentType,fileName);
//		return dataSource;
//	}

	public long writeTo(OutputStream out,String locale) throws IOException {
		long size=0;
		if (fileName != null) {
			size = write( out );
		}
		return size;
	}

	//file write
	long write(OutputStream out) throws IOException {
		if (contentType.equals("application/x-macbinary")) {
			out = new MacBinaryDecoderOutputStream(out);
		}
		long size=0;
		int read;
		byte[] buf = new byte[8 * 1024];
		while((read = partInput.read(buf)) != -1) {
			out.write(buf, 0, read);
			size += read;
		}
		return size;
	}

	public boolean isFile() {
		return true;
	}
	private String asc2euc(String asc,String locale ) throws java.io.UnsupportedEncodingException {
		String encodingType=null;
		if(asc == null) {
			return "";
		}
    	if(locale.equals("ko")){
        	encodingType = "EUC-KR";
        }else if(locale.equals("ja")){
        	encodingType = "Shift_JIS";
        }
        return new String (asc.getBytes("8859_1"), encodingType);
        //return new String (asc.getBytes(encodingType), encodingType);
        //return new String (asc.getBytes(), encodingType);
	}

	private String checkAndGetFileName(File dir, String name) {

		int idx = name.lastIndexOf(".");
		String pref = name.substring(0, idx);
		String sur = name.substring(idx);
		File file = new File(dir, name);
		int count = 0;
		while(file.exists()) {
			name = pref+"[" + ++count +"]"+ sur;
			file = new File(dir , name);
		}
		return name;
	}

}
728x90
์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'Languages/JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • zipํŒŒ์ผ ํ•ด์ œ, ๋Œ€์ƒ ํด๋” ๋ณ€๊ฒฝ & ํŒŒ์ผ ์œ„์น˜ ๋ณ€๊ฒฝ
  • [java] ์ฃผ๋ง ์ œ์™ธํ•œ ์ด์ „ 5์ผ ๊ตฌํ•˜๊ธฐ
  • [JAVA]ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
  • [java]ํŠน์ˆ˜๋ฌธ์ž์˜ ์กด์žฌ์—ฌ๋ถ€
์ง€๋‹ˆ๐Ÿงž‍โ™‚๏ธ๐Ÿฅญ
์ง€๋‹ˆ๐Ÿงž‍โ™‚๏ธ๐Ÿฅญ
์ผ์ƒ, ๊ฒŒ์ž„, ๋ง›์ง‘, ์—ฌํ–‰, ๊ฐœ๋ฐœ, IT ๋ธ”๋กœ๊ทธ๐Ÿงž
  • ์ง€๋‹ˆ๐Ÿงž‍โ™‚๏ธ๐Ÿฅญ
    ์š”์ˆ  ๋žจํ”„๐Ÿซ–
    ์ง€๋‹ˆ๐Ÿงž‍โ™‚๏ธ๐Ÿฅญ
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • All (549)
      • Languages (57)
        • JAVA (13)
        • JSP (1)
        • C_C++ (4)
        • Html (3)
        • CSS (1)
        • JavaScript (18)
        • Python (3)
        • Kotlin (13)
        • TypeScript (1)
      • Framework (14)
        • spring (11)
        • jstl (1)
        • angular (2)
      • Tool (28)
        • Eclipse (5)
        • vsCode (3)
        • scrcpy (2)
        • Git (1)
        • IntelliJ (6)
        • Visual-studio (1)
        • UML (1)
        • Gradle (8)
      • DB (6)
        • Oracle (1)
        • MySql (3)
        • Mongo (2)
      • OS (14)
        • Linux (2)
        • Windows (12)
      • Server (8)
        • Tomcat (1)
        • Apache (1)
        • Node.js (6)
      • Programmings (25)
        • Design Pattern (2)
        • Funny (20)
        • Algorithms (3)
      • Cloud (8)
        • Docker (1)
        • Kubernetes (4)
        • Istio (1)
        • ArgoCD (2)
      • IT (5)
        • gRPC (3)
        • RESTful (3)
        • Web UI (5)
        • AI (4)
      • Book (6)
      • TIP (187)
      • Life (53)
      • Game (83)
      • Storage (22)
      • ์‹๋‹น (15)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
    • ์‚ฌ์ดํŠธ๋งต
    • RSS
    • ๊ธฐํƒ€ ์†Œ๋“
  • ๋งํฌ

    • ๊ตฌ๊ธ€
    • ๋„ค์ด๋ฒ„
    • ์ •๋ถ€24
    • Spring Framework ๋ฆด๋ฆฌ์ฆˆ ๋…ธํŠธ
    • Kotlin ๋ฆด๋ฆฌ์ฆˆ ๋…ธํŠธ
    • ์นด์นด์˜ค ์• ๋“œํ•
    • ๋ธ”๋กœ๊ทธ ์‚ฌ์ดํŠธ๋งต
    • ๋ธ”๋กœ๊ทธ RSS
  • ๊ณต์ง€์‚ฌํ•ญ

    • ์•ˆ๋…•ํ•˜์„ธ์š”
  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ํ•œ๋ˆˆ์— ๋ณด๋Š” ์˜ค๋Š˜์˜ ๋‰ด์Šค
    ๋ชฌ์Šคํ„ฐํ—Œํ„ฐ๋‚˜์šฐ
    ํ€˜์ŠคํŠธ
    ๋‰ด์Šค
    ๋‰ด์Šค ๋ธŒ๋ฆฌํ•‘
    ํƒœ๊ตญ
    ๋ชฌ์Šคํ„ฐํ—Œํ„ฐ์™€์ผ์ฆˆ
    ์˜ค๋Š˜์˜๋‰ด์Šค
    ๋ฐ์ผ๋ฆฌ ๋‰ด์Šค
    ๋ธŒ๋ฆฌํ•‘
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
์ง€๋‹ˆ๐Ÿงž‍โ™‚๏ธ๐Ÿฅญ
[java]FilePart(fileUpload)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”