1 23 24 package com.sun.enterprise.deployment.util; 25 26 import java.io.*; 27 import java.net.*; 28 import java.util.*; 29 30 import sun.misc.BASE64Encoder; 31 32 import com.sun.enterprise.deployment.deploy.shared.Archive; 33 import com.sun.enterprise.deployment.deploy.shared.MemoryMappedArchive; 34 35 36 public class FileUploadUtil { 37 private static final String JSR88 = "jsr88"; 38 39 50 public static String uploadToServlet(String host, String port, String user, 51 String password, Archive module) throws Exception { 52 53 Socket socket = null; 54 BufferedInputStream bis = null; 55 56 try { 57 58 String fileName = null; 59 if (module instanceof MemoryMappedArchive) { 60 byte[] bytes = ((MemoryMappedArchive) module).getByteArray(); 62 bis = new BufferedInputStream(new ByteArrayInputStream(bytes)); 63 File tmpFile = File.createTempFile(JSR88, null); 64 tmpFile.deleteOnExit(); 65 fileName = tmpFile.getName(); 66 } else { 67 File f = new File(module.getURI().getPath()); 69 bis = new BufferedInputStream(new FileInputStream(f)); 70 fileName = f.getName(); 71 } 72 73 74 String path = "/web1/uploadServlet?file=" + URLEncoder.encode(fileName, "UTF-8"); 76 77 socket = new Socket(host, Integer.parseInt(port)); 78 79 OutputStream out = socket.getOutputStream(); 80 InputStream in = socket.getInputStream(); 81 82 BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); 84 wr.write("POST " + path + " HTTP/1.0\r\n"); 85 wr.write("Content-Length: " + bis.available() + "\r\n"); 86 wr.write("Content-Type: application/octet-stream\r\n"); 87 88 byte[] encodedPassword = (user + ":" + password).getBytes(); 90 BASE64Encoder encoder = new BASE64Encoder(); 91 wr.write("Authorization: Basic " + encoder.encode(encodedPassword) + "\r\n"); 92 wr.write("\r\n"); 93 wr.flush(); 94 95 byte[] buffer = new byte[1024*64]; 97 for (int i = bis.read(buffer); i > 0; i = bis.read(buffer)) { 98 out.write(buffer, 0, i); 99 } 100 out.flush(); 101 102 BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); 104 Vector v = new Vector(); 105 for (int timeoutCounter = 0; timeoutCounter < 120; timeoutCounter++) { 106 boolean finished = false; 107 while (reader.ready()) { 108 String curLine = reader.readLine(); 109 v.add(curLine); 110 if (curLine.startsWith("SUCCESS:")) { 111 finished = true; 112 } 113 } 114 if (finished) { 115 break; 116 } else { 117 Thread.sleep(500); 118 } 119 } 120 121 if (v.size() == 0) { 122 throw new Exception ("Upload file failed: no server response received"); 123 } 124 125 boolean isData = false; 127 int i = 0; 128 String responseString = null; 129 for (Enumeration e = v.elements(); e.hasMoreElements(); i++) { 130 String curElement = (String ) e.nextElement(); 131 132 if (i == 0) { 134 String responseCode = curElement.substring(curElement.indexOf(" ") + 1); 135 if (!responseCode.startsWith("200")) { 136 throw new Exception ("HTTP connection failed: " + responseCode); 137 } 138 } 139 if (curElement.equals("")) { 141 isData = true; 142 continue; 143 } 144 if (isData) { 146 responseString = curElement; 147 break; 148 } 149 } 150 151 if (responseString == null) { 152 throw new Exception ("Upload file failed: no server response received"); 153 } 154 155 if (!responseString.startsWith("SUCCESS:")) { 156 throw new Exception ("Upload file failed: " + responseString); 157 } 158 159 String remotePath = responseString.substring(8); 161 162 return remotePath; 163 164 } catch (Exception e) { 165 throw e; 166 } finally { 167 if (socket != null) { 168 socket.close(); 169 } 170 if (bis != null) { 171 bis.close(); 172 } 173 } 174 175 } 176 177 } 178 | Popular Tags |