import com.haulmont.cuba.gui.components.AbstractWindow; import org.apache.http.entity.FileEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpGet; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.DefaultHttpClient; import java.io.File; import java.io.IOException; import java.io.InterruptedIOException; import java.io.ByteArrayOutputStream; public class Screen extends AbstractWindow { public void onButtonClick() { String hostIP = "192.168.0.120"; String sessionUUUID = ""; ByteArrayOutputStream out = new ByteArrayOutputStream(); String query = "http://" + hostIP + ":8080/app/dispatch/api/login?u=admin&p=admin&l=ru"; HttpGet authorization = new HttpGet(query); HttpClient httpClient = new DefaultHttpClient(); try { HttpResponse httpResponse = httpClient.execute(authorization); httpResponse.getEntity().writeTo(out); sessionUUUID = out.toString(); } catch (IOException e) { } finally { httpClient.getConnectionManager().shutdown(); } File file = new File("C:\\Test.txt"); FileEntity entity = new FileEntity(file); String ext = getFileExtension(file); String url = "http://" + hostIP + ":8080/app-portal/api/upload?s="+sessionUUUID+"&name="+ file.getName() +"&ext=" + ext; HttpPost method = new HttpPost(url); method.setEntity(entity); HttpClient client = new DefaultHttpClient(); try { HttpResponse response = client.execute(method); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { showNotification("Файл загружен " + url + " " + response.getStatusLine()); } else { response.getEntity().writeTo(out); showNotification(out.toString()); } } catch (InterruptedIOException e) { showNotification("Uploading has been interrupted"); //throw e; } catch (IOException e) { showNotification("Unable to upload file to " + url + " " + e); } finally { client.getConnectionManager().shutdown(); } } private static String getFileExtension(File file) { String fileName = file.getName(); if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) return fileName.substring(fileName.lastIndexOf(".")+1); else return ""; } }