Re: Prenos velkych suborov do z klienta do webservice

tapik na buchtovi.cz tapik na buchtovi.cz
Úterý Duben 21 21:05:58 CEST 2009


Ja bych to jenom malinko rozsiril - jak spravne Makub naznacuje, WebServices != SOAP.
Na druhou stranu bych ale FTP za WS taky nepovazoval. Jako velmi prinosne bych videl zabalit soubor do MIME obalky - toto rozsireni necht si laskavy ctenar udela jako DU ;-)  Mala napoveda: muzete pouzit treba http://james.apache.org/mime4j/ ;-)
 
> ------------ Původní zpráva ------------
> Od: Martin Kuba <makub na ics.muni.cz>
> Předmět: Re: Prenos velkych suborov do z klienta do webservice
> Datum: 07.4.2009 11:52:20
> ----------------------------------------
> Dusan Zatkovsky napsal(a):
> > S tym servletom je to vyborny napad, akurat vobec netusim, ako to 
> > naprogramovat ( web je moja slaba stranka ) :)
> 
> Tak tady to je :-)
> 
> public class PutServlet extends HttpServlet {
>      @Override
>      protected void doPut(HttpServletRequest request, 
> HttpServletResponse response) throws ServletException, IOException {
> 
>          ServletInputStream in = request.getInputStream();
>          byte[] buffer = new byte[4096];
>          BufferedOutputStream out = new BufferedOutputStream(new 
> FileOutputStream("/tmp/soubor"));
>          int n;
>          while((n=in.read(buffer))!=-1) {
>              out.write(buffer,0,n);
>          }
>          out.close();
>          in.close();
>          response.setStatus(HttpServletResponse.SC_OK);
>      }
> }
> 
> a namapovat ho ve web.xml třeba takto:
> 
>      <servlet>
>          <servlet-name>PutServlet</servlet-name>
>          <servlet-class>cz.makub.PutServlet</servlet-class>
>      </servlet>
>      <servlet-mapping>
>          <servlet-name>PutServlet</servlet-name>
>          <url-pattern>/put/*</url-pattern>
>      </servlet-mapping>
> 
> a klient může použít HTTP klienta z JDK:
> 
> public class PutClient {
>      public static void main(String[] args) throws IOException {
>          HttpURLConnection uc = (HttpURLConnection) new 
> URL("http://localhost:8080/putapp/put/file").openConnection();
>          uc.setRequestMethod("PUT");
>          uc.setDoOutput(true);
>          BufferedInputStream in = new BufferedInputStream(
>                  new FileInputStream("veliky_soubor"));
>          byte[] buffer = new byte[4096];
>          int n;
>          OutputStream out = uc.getOutputStream();
>          while ((n = in.read(buffer)) != -1) {
>              out.write(buffer, 0, n);
>          }
>          in.close();
>          out.close();
>          int responseCode = uc.getResponseCode();
>          System.out.println("responseCode = " + responseCode);
>      }
> }
> 
> Samozřejmě čtení a ukládání souborů je třeba předělat
> na něco reálného.
> 
> Makub
> -- 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Supercomputing Center Brno             Martin Kuba
> Institute of Computer Science    email: makub na ics.muni.cz
> Masaryk University             http://www.ics.muni.cz/~makub/
> Botanicka 68a, 60200 Brno, CZ     mobil: +420-603-533775
> --------------------------------------------------------------
> 
> 
> 
> 
> 

Oto 'tapik' Buchta, tapik na buchtovi.cz



Další informace o konferenci Konference