Jump to content

Recommended Posts

Posted

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?

Posted

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!");
        }



    }
Posted
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."

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.