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 파일을 선택하고 테스트 웹 서비스를 선택하고 해당하는 방법의 매개 변수를 입력하면 됩니다.
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기