The examples in this article share the code for uploading and downloading of java webservice for your reference. The specific content is as follows
1. Create a new dynamic web project youmeFileServer, create a new package com, and create a 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 and download file*/public class FileProgress{ public String saysHello(String name) { return "Hello," + name+"/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); // Write the data in the byte array bytes to the file output stream 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()]; // From the input stream in, read bytes of data of bytes.length into the byte array 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; } /* * Get the current time*/ 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());//Get the current time return formatter.format(curDate); } catch(Exception ex) { System.out.println(ex.getMessage()); return ""; } } /* * Get the current date*/ private static String getDataString() { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date curDate = new Date(System.currentTimeMillis());//Get the current time return formatter.format(curDate); } catch(Exception ex) { System.out.println(ex.getMessage()); return ""; } } } 2. Publish 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, and select the method interface to be published in the next step until it is completed. Tomcat has started. Right-click the wsdl file in the wsdl folder just generated, select Test webservice, and enter the parameters of the corresponding method.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.