package webserver;
public class ContentInfo {
// content-length: 3266
// content-type: image/png
private long contentLength;
private String contentType;
public ContentInfo(long contentLength, String contentType){
this.contentLength = contentLength;
this.contentType = contentType;
}
// getter / setter
public long getContentLength() {
return contentLength;
}
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
// content-length: 3266
// content-type: image/png
public String toString(){
String str = "content-length " + contentLength + "\n";
str += "content-type: " + contentType + "\n";
return str;
}
}
package webserver;
import java.io.File;
import java.nio.file.Files;
public class FileUtil {
// baseDir는 /로 끝나지 않는다.
private String baseDir;
public FileUtil(String baseDir) {
this.baseDir = baseDir;
}
// filePath는 /로 시작된다.
public ContentInfo getContentInfo(String filePath){
//File
File file = new File(this.baseDir + filePath);
if(!file.exists())
return null;
String contentType = null;
try {
contentType = Files.probeContentType(file.toPath());
}catch(Exception ex) {
return null;
}
ContentInfo contentInfo = new ContentInfo(file.length(), contentType);
return contentInfo;
}
}
package webserver;
public class FileUtilTest {
public static void main(String[] args) {
FileUtil fileUtil = new FileUtil("C:\\Users\\handa\\Desktop\\file");
ContentInfo contentInfo = fileUtil.getContentInfo("/my/hello.txt");
System.out.println(contentInfo);
}
}
해당 경로에 파일이 있을 경우
해당 경로에 파일이 없을 경우
stackoverflow의 답변..
public class ContentInfo{
...~~~
...~~~
...~~~
public boolean isBinaryType(){
if(contentType == null ){
return true;
} else if(contentType.startsWith("text")){
return false;
} else {
// type isn't text
return true;
}
}
}
라고 물으신다면, 대답해주는게 인지상정
file이 binaryType인지 아닌지에 따라 bite단위로 읽어들이는지, char단위로 읽어들이는지 알 수있다.
package webserver;
public class ContentInfo {
// content-length: 3266
// content-type: image/png
private String filePath;
private long contentLength;
private String contentType;
public ContentInfo(String filePath,long contentLength, String contentType){
this.filePath = filePath;
this.contentLength = contentLength;
this.contentType = contentType;
}
// getter / setter
public long getContentLength() {
return contentLength;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
// content-length: 3266
// content-type: image/png
public String toString(){
String str = "content-length " + contentLength + "\n";
str += "content-type: " + contentType + "\n";
return str;
}
public boolean isBinaryType(){
if(contentType == null ){
return true;
} else if(contentType.startsWith("text")){
return false;
} else {
// type isn't text
return true;
}
}
}
package webserver;
import java.io.File;
import java.nio.file.Files;
public class FileUtil {
// baseDir는 /로 끝나지 않는다.
private String baseDir;
public FileUtil(String baseDir) {
this.baseDir = baseDir;
}
// filePath는 /로 시작된다.
public ContentInfo getContentInfo(String filePath){
//File
File file = new File(this.baseDir + filePath);
if(!file.exists())
return null;
String contentType = null;
try {
contentType = Files.probeContentType(file.toPath());
}catch(Exception ex) {
return null;
}
ContentInfo contentInfo = new ContentInfo(file.getAbsolutePath(),file.length(), contentType);
return contentInfo;
}
}
ContentInfo contentInfo = new ContentInfo(file.getAbsolutePath(),file.length(), contentType);
public void copy(ContentInfo contentInfo, OutputStream out){
try(FileInputStream in = new FileInputStream(contentInfo.getFilePath())){
byte[] buffer = new byte[512];
// byte로 읽어들인다.
// 읽어들인 건수.,
int readCount = 0;
// buffer에다 읽어서, 일
while((readCount = in.read(buffer)) != -1){
out.write(buffer,0,readCount);
}
} catch(Exception ex){
ex.printStackTrace();
}
}
public void copy(ContentInfo contentInfo, Writer out){
try(FileReader in = new FileReader(contentInfo.getFilePath())){
char[] buffer = new char[512];
// byte로 읽어들인다.
// 읽어들인 건수.,
int readCount = 0;
// buffer에다 읽어서, 일
while((readCount = in.read(buffer)) != -1){
out.write(buffer,0,readCount);
}
} catch(Exception ex){
ex.printStackTrace();
}
}
package webserver;
import java.io.FileOutputStream;
public class FileUtilTest {
public static void main(String[] args) {
FileUtil fileUtil = new FileUtil("C:\\Users\\handa\\Desktop\\file");
ContentInfo contentInfo = fileUtil.getContentInfo("/my/ourheung.png");
System.out.println(contentInfo);
//FileOutputStream을 얻어온다.
try(FileOutputStream out = new FileOutputStream("C:\\Users\\handa\\Desktop\\file/my/ourheung2.png")){
fileUtil.copy(contentInfo, out);
// contentInfo정보에 읽어서 out을 대상으로 써줘라
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
try(FileOutputStream out = new FileOutputStream("C:\\Users\\handa\\Desktop\\file/my/ourheung2.png")){
fileUtil.copy(contentInfo, out);
}catch(Exception ex){
ex.printStackTrace();
}
}
package webserver;
import java.io.FileWriter;
public class FileUtilTest {
public static void main(String[] args) {
FileUtil fileUtil = new FileUtil("C:\\Users\\handa\\Desktop\\file");
ContentInfo contentInfo = fileUtil.getContentInfo("/my/hello.txt");
System.out.println(contentInfo);
try(FileWriter out = new FileWriter("C:\\Users\\handa\\Desktop\\file/my/hello2.txt")){
fileUtil.copy(contentInfo, out);
// contentInfo정보에 읽어서 out을 대상으로 써줘라
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
try(FileWriter out = new FileWriter("C:\\Users\\handa\\Desktop\\file/my/hello2.txt")){
fileUtil.copy(contentInfo, out);
// contentInfo정보에 읽어서 out을 대상으로 써줘라
}catch(Exception ex) {
ex.printStackTrace();
}