public class FileUpload {
private static String getAlfticket() throws IOException, JSONException {
String ticket = "";
URL url = new URL("http://localhost:8080/alfresco/s/api/login?u=admin&pw=admin&format=json");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String json = IOUtils.toString(in, encoding);
JSONObject getData = new JSONObject(json);
ticket =getData.getJSONObject("data").get("ticket").toString();
return ticket;
}
public static void main(String[] args) {
String ticketAlf=null;
try {
ticketAlf = FileUpload.getAlfticket(); // Getting a ticketAlf via custom method
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String url = "http://localhost:8080/alfresco/service/api/upload"; // URL of API that needs to be called !!!
File file = new File("D:/anrose.txt"); //The file that needs to be uploaded !!
MultiValueMap<String, Object> request = new LinkedMultiValueMap<String, Object>();
FileSystemResource rsc = new FileSystemResource("D:/anrose.txt");
request.add("filedata", rsc);
request.add("siteid", "google");
request.add("containerid", "documentLibrary");
request.add("uploaddirectory", "test");
request.add("alf_ticket","TICKET_5e1bff6214cd28abf179d5398a64ec89d83a2c0a");
RestTemplate rest = new RestTemplate();
rest.postForEntity(url, request, String.class); // Here it gives the error
}
}