java 웹 서비스 다운로드 파일 코드 공유 업로드
1. 새 동적 웹 프로젝트 youmeFileServer, 새 패키지com, 새 클래스 FileProgress
package com;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Random;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/*
* web servcie
*/
public class FileProgress
{
public String sayHello(String name)
{
return "Hello," + name+"
"+getdir("2");
}
/*
*
*/
public String uploadFile(String fileName, String filetype, String file)//byte[] bytes)
{
FileOutputStream fos = null;
try
{
String filedir = getdir(filetype);
BASE64Decoder decoder= new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(file);
if(filedir=="")
{
return "";
}
Integer rdm = new Random().nextInt(10000);
String savename = getDataTimeString(true) +rdm.toString()+fileName.substring(fileName.indexOf('.'));
fos = new FileOutputStream(filedir+savename);
// bytes , fos
fos.write(bytes);
fos.flush();
return filedir +savename;
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
finally
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* @param filepath
*/
private String getdir(String filetype)
{
String path = "F:\\youme\\{0}\\" + getDataString()
+ "\\";
switch (filetype)
{
case "2":
path = path.replace("{0}", "image");
break;
case "3":
path = path.replace("{0}", "vedio");
break;
default:
return "";
}
try
{
java.io.File file = new java.io.File(path);
if(!file.exists())
{
if(!file.mkdirs())
{
return "";
}
}
return path;
}
catch(Exception ex)
{
return "";
}
finally
{
}
}
/*
*
*/
public String downloadFile(String filepath)
{
// filepath = "F:\\youme\\vedio\\2013-09-03\\201309031700143294.amr";
FileInputStream in = null;
byte bytes[] = null;
String file = null;
try
{
in = new FileInputStream(filepath);
bytes = new byte[in.available()];
// in , bytes.length bytes
in.read(bytes);
BASE64Encoder encoder = new BASE64Encoder();
file = encoder.encode(bytes);
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
// return bytes;
return file;
}
/*
*
*/
private static String getDataTimeString(Boolean isfilename)
{
try
{
SimpleDateFormat formatter = null;
if(!isfilename)
{
formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
else
{
formatter= new SimpleDateFormat("yyyyMMddHHmmss");
}
Date curDate = new Date(System.currentTimeMillis());//
return formatter.format(curDate);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
return "";
}
}
/*
*
*/
private static String getDataString()
{
try
{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date curDate = new Date(System.currentTimeMillis());//
return formatter.format(curDate);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
return "";
}
}
}
2. 테스트 웹 서비스 게시(eclipse Java EE IDE)방금 만들어진 클래스를 오른쪽 단추로 누르면 현재 웹 서비스-->create 웹 서비스, 웹 서비스 type은bottom upjava bean 웹 서비스, 서비스implementation은 목표 클래스를 선택하고 다음 단계는 공개될 방법 인터페이스를 선택하여 완료할 때까지tomcat이 시작되었습니다. 오른쪽 단추로 만들어진 wsdl 폴더 안의 wsdl 파일을 선택하고 테스트 웹 서비스를 선택하고 해당하는 방법의 매개 변수를 입력하면 됩니다.
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.