Alguém jah conseguiu fazer?
Estou tentando utilizar o código abaixo, mas não funciona. Alguém tem alguma sugestão?? import java.io.DataInputStream; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; // This class provides a static method to post a file to the // putfile script, which takes a filename as a parameter passed // in the POST request itself, and then receives the bytes as // the posted data. public class PostPutFile { // Put sends the named file to a specific URL. The URL should // contain the path name of the putfile script. This method // will append the ?filename to the script name. // It returns 0 if the put was successful, or a non-zero number // if it failed for some reason. public static int put(URL url, String filename, byte[] bytes) throws IOException, MalformedURLException { // Run the putfile script and ask it to store the data in a file called "putme" URL destURL = new URL( url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "?" + filename); System.out.println("Protocol: "+ url.getProtocol()); System.out.println("Host : "+ url.getHost()); System.out.println("Port: "+ url.getPort()); System.out.println("File "+ url.getFile() + "?" + filename); // Define the data that you want stored in the file. URLConnection urlConn = destURL.openConnection(); urlConn.setDoOutput(true); // we need to write urlConn.setDoInput(true); // just to be safe... urlConn.setUseCaches(false); // get info fresh from server // Tell the server what kind of data we are sending - in this case, // just a stream of bytes. urlConn.setRequestProperty("Content-type", "application/octet-stream"); // Must tell the server the size of the data we are sending. This also // tells the URLConnection class that we are doing a POST instead // of a GET. urlConn.setRequestProperty("Content-length", "" + bytes.length); // Open an output stream so we can send the info we are posting OutputStream outStream = urlConn.getOutputStream(); // Write out the actual request data outStream.write(bytes); outStream.close(); // Now that we have sent the data, open up an input stream and get // the response back from the server DataInputStream inStream = new DataInputStream(urlConn.getInputStream()); String line = inStream.readLine(); inStream.close(); try { int result = Integer.valueOf(line).intValue(); return result; } catch (Exception parseError) { return -1; } } public static void main(String args[]) throws IOException, MalformedURLException { try { URL destURL = new URL("http://test:8080/upload/jsp/upload.jsp"); // Define a string we want to send int lengthFile; File dataToSend = new File ("PostPutFile.java") ; lengthFile = (int) dataToSend.length(); // The PostPutFile class wants a byte array, however, so we convert // the string to a byte array. byte[] bytes = new byte[lengthFile ]; dataToSend.getBytes(0, lengthFile , bytes, 0); PostPutFile.put(destURL, "PostPutFile.java", bytes); } catch (Exception e) { e.printStackTrace(); } } } Grato |