BlueDragon Posted June 9, 2009 at 11:23 AM Report #270977 Posted June 9, 2009 at 11:23 AM Boas a todos, Precisava de saber como tratar o metodo put num server que estou a desenvolver. a intenção é o método aquando de um browser requisitar o metodo put. alguém tem algum pedaço de codigo que possa ajudar?
BlueDragon Posted June 15, 2009 at 01:11 PM Author Report #272388 Posted June 15, 2009 at 01:11 PM Boas, o problema está quase resolvido, já passa alguns tipos de ficheiros, mas imagens ou docs não. penso que o problema esteja no método usado para ler o conteudo do ficheiro. Se alguem conseguir ajudar. public class Put { int porta = 666; String ip = "172.18.3.199"; String pasta = "/"; Put() { try { File file = new File(pasta, "images.jpgl"); byte[] mybytearray = new byte[(int) file.length()]; FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); bis.read(mybytearray, 0, mybytearray.length); BufferedReader reader = new BufferedReader(new FileReader(file)); StringBuilder sb = new StringBuilder(); while (reader.ready()) { char c = (char) reader.read(); sb.append(c); } System.out.println("File: " + sb); Socket cliente = new Socket(ip, porta); PrintWriter out = new PrintWriter(cliente.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(cliente.getInputStream())); String put = "PUT "+file.getName()+" HTTP/1.1\r\n"; put = put + "Content-Lenght: "+file.length()+"\r\n"; put = put + "\r\n"; put = put + sb; out.println(put); out.flush(); String line; String resposta = ""; while ((line = in.readLine()) != null) { resposta += line + "\n"; } System.out.printf(resposta); out.close(); in.close(); cliente.close(); } catch (Exception exception) { System.out.println("problemas da treta!"); } }
M6 Posted June 15, 2009 at 01:15 PM Report #272390 Posted June 15, 2009 at 01:15 PM Experimenta usar os streams de byte array: de http://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayInputStream.html e http://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayOutputStream.html Edit: e daí talvez não seja isso que necessitas. Vê estes exemplos: - http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html - http://www.java2s.com/Code/JavaAPI/javax.net/SocketgetInputStream.htm 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now