Java WebService upload download file code share _java

Source: Internet
Author: User
Tags current time getmessage wsdl

This example for you to share the Java WebService upload download file code for your reference, the specific content as follows

1, new Dynamic Web Engineering youmefileserver, new package COM, inside new class 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 upload download/public class Fileprogress {public string SayHello (string name) {return "Hello," + N
  Ame+ "\ n" +getdir ("2");
    * * * File Upload service/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); Writes the data in the byte array bytes to the file lossOut Stream Fos Middle 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 {}}/* File download service   */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 ()];
      Reads the bytes.length byte data into the byte array bytes in.read (bytes) from the input stream in;
      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; * * * Get current time/private static String getdatatimestring (Boolean isfilename) {try {Simpledatef
      Ormat formatter = null; 
      if (!isfilename) {formatter= new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
      } else{formatter= new SimpleDateFormat ("Yyyymmddhhmmss"); 
    Date curdate = new Date (System.currenttimemillis ());//Get Current time return Formatter.format (curdate);
      catch (Exception ex) {System.out.println (Ex.getmessage ());
    Return "";  }/* * Get current date/private static String getdatastring () {try {SimpleDateFormat formatter =   
      New SimpleDateFormat ("Yyyy-mm-dd"); 
    Date curdate = new Date (System.currenttimemillis ());//Get Current time return Formatter.format (curdate);
      catch (Exception ex) {System.out.println (Ex.getmessage ());
    Return ""; 
 }
  }  
  
}

2. Release Test WebService (Eclipse Java EE IDE)

Right-click the class you just created, now webservice-->create webservice,webservice type Select bottom Upjava bean Web Service,service implementation Select the target class, the next step is to publish the Open method interface, until completed, Tomcat has started, right just generated WSDL folder inside the WSDL file, select Test WebService, enter the corresponding method parameters.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.