How to download and upload file from SharePoint Server in Java? - java

I am trying to write a simple example code with the help of this java-sharepoint-library
library but still i am not able to design simple program.

For development used information - https://paulryan.com.au/2014/spo-remote-authentication-rest/
Everything is well described there.
import java.io.*;
import java.net.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.Document;
import org.xml.sax.*;
public class LoginManagerSharePoint {
private final String sts = "https://login.microsoftonline.com/extSTS.srf";
private final String loginContextPath = "/_forms/default.aspx?wa=wsignin1.0";
//private final String contextInfoPath = "/_api/contextinfo";
private final String sharepointContext = "xxxxxxx";
private final String reqXML = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" " +
"xmlns:a=\"http://www.w3.org/2005/08/addressing\" " +
"xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">" +
"<s:Header><a:Action s:mustUnderstand=\"1\">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue" +
"</a:Action><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>" +
"</a:ReplyTo><a:To s:mustUnderstand=\"1\">https://login.microsoftonline.com/extSTS.srf</a:To>" +
"<o:Security s:mustUnderstand=\"1\" " +
"xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">" +
"<o:UsernameToken><o:Username>[username]</o:Username><o:Password>[password]</o:Password>" +
"</o:UsernameToken></o:Security></s:Header><s:Body><t:RequestSecurityToken " +
"xmlns:t=\"http://schemas.xmlsoap.org/ws/2005/02/trust\">" +
"<wsp:AppliesTo xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">" +
"<a:EndpointReference><a:Address>[endpoint]</a:Address></a:EndpointReference>" +
"</wsp:AppliesTo><t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey" +
"</t:KeyType><t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue" +
"</t:RequestType><t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>" +
"</t:RequestSecurityToken></s:Body></s:Envelope>";
private String generateSAML() {
String saml = reqXML
.replace("[username]", "username");
saml = saml.replace("[password]", "password");
saml = saml.replace("[endpoint]", String.format("https://%s.sharepoint.com/_forms/default.aspx?wa=wsignin1.0", sharepointContext));
return saml;
}
public String getCookie() {
String token;
try {
token = requestToken();
String cookie = submitToken(token);
//System.out.println(cookie);
return cookie;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private String requestToken() throws XPathExpressionException, SAXException,
ParserConfigurationException, IOException {
String saml = generateSAML();
URL u = new URL(sts);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
// http://stackoverflow.com/questions/12294274/mobile-app-for-sharepoint/12295224#12295224
connection.addRequestProperty("Content-Type",
"text/xml; charset=utf-8");
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
wout.write(saml);
wout.flush();
wout.close();
InputStream in = connection.getInputStream();
int c;
StringBuilder sb = new StringBuilder("");
while ((c = in.read()) != -1)
sb.append((char) (c));
in.close();
String result = sb.toString();
String token = extractToken(result);
//System.out.println(token);
return token;
}
private String extractToken(String result) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
//http://stackoverflow.com/questions/773012/getting-xml-node-text-value-with-java-dom
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new InputSource(new StringReader(result)));
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
String token = xp.evaluate("//BinarySecurityToken/text()", document.getDocumentElement());
//handle error S:Fault:
//http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/df862099-d9a1-40a4-b92e-a107af5d4ca2
//System.out.println(token);
return token;
}
private String submitToken(String token) throws IOException {
// http://cafeconleche.org/books/xmljava/chapters/ch03s05.html
String url = String.format("https://%s.sharepoint.com%s", sharepointContext, loginContextPath);
URL u = new URL(url);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/x-www-form-urlencoded");
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)");
// http://stackoverflow.com/questions/12294274/mobile-app-for-sharepoint/12295224#12295224
connection.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setInstanceFollowRedirects(false);
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
wout.write(token);
wout.flush();
wout.close();
InputStream in = connection.getInputStream();
//http://www.exampledepot.com/egs/java.net/GetHeaders.html
String setCookieRtFa = null;
String setCookieFedAuth = null;
for (int i=0; ; i++) {
String headerName = connection.getHeaderFieldKey(i);
String headerValue = connection.getHeaderField(i);
//System.out.println("header: " + headerName + " : " + headerValue);
if (headerName == null && headerValue == null) {
// No more headers
break;
}
if (headerName == null) {
// The header value contains the server's HTTP version
}
if (headerName != null) {
if (headerName.equals("Set-Cookie")) {
if (setCookieRtFa == null) {
setCookieRtFa = headerValue;
} else {
int t = 0;
if (headerValue.equals("RpsContextCookie=; path=/")) t = 1;
if (t == 0) {
setCookieFedAuth = headerValue;
}
}
}
}
}
String cookieContainer = setCookieRtFa.split("\\;")[0] + ";" + setCookieFedAuth.split("\\;")[0];
in.close();
return cookieContainer;
}
}
LoginManagerSharePoint loginManagerSharePoint = new LoginManagerSharePoint();
String cookieContainer = loginManagerSharePoint.getCookie();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(URL_FILE);
httpGet.addHeader("Cookie", cookieContainer);
httpGet.addHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)");
HttpResponse response = httpClient.execute(httpGet);
InputStream inputStream = response.getEntity().getContent();

Related

Java send POST?

I try to send POST data to a Website. But the "login" will not work.
Slowly i dont know why. There are quite a lot of ways to login to a website without browser-ui. What is the "best" method to login here ?
I uses Jsoup and "normal" HttpURLConnection. With Selenium it works fine, but very slow =(
import com.gargoylesoftware.htmlunit.util.Cookie;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.util.List;
import java.util.Map;
public class postget {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String GET_URL = "https://de.metin2.gameforge.com/";
private static final String POST_URL = "https://de.metin2.gameforge.com:443/user/login?__token=";//+token
private static final String POST_PARAMS = "username=USERNAME"+"password=PASSWORD";
static final String COOKIES_HEADER = "Set-Cookie";
public static void main(String[] args) throws IOException {
String var = sendGET();
String var1 =var.substring(0,var.indexOf(";"));
String var2 =var.substring(var.indexOf(";")+1,var.indexOf(":"));
String token =var.substring(var.indexOf(":")+1);
//sendPOST(token,cookie);
jsoup(cookie(),token);
}
private static String sendGET() throws IOException {
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String veri1=response.substring(response.indexOf("verify-v1")+20,response.indexOf("verify-v1")+63);
String veri2=response.substring(response.indexOf("verify-v1")+104,response.indexOf("verify-v1")+147);
String token=response.substring(response.indexOf("token")+6,response.indexOf("token")+38);
return (veri1+";"+veri2+":"+token);
} else {
System.out.println("GET request not worked");
return " ";
}
}
private static String cookie() throws IOException{
String realCookie="";
URL obj = new URL(GET_URL);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
java.net.CookieManager msCookieManager = new java.net.CookieManager();
Map<String, List<String>> headerFields = connection.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
if (cookiesHeader != null) {
for (String cookie : cookiesHeader) {
msCookieManager.getCookieStore().add(null,HttpCookie.parse(cookie).get(0));
}
}
List<HttpCookie> cookiess = msCookieManager.getCookieStore().getCookies();
if (cookiess != null) {
if (cookiess.size() > 0) {
for (HttpCookie cookie : cookiess) {
realCookie = cookie.toString().substring(cookie.toString().indexOf("=")+1);
return(realCookie);
}
}
}
return(realCookie);
}
private static void sendPOST(String token,CookieManager msCookieManager) throws IOException {
URL obj = new URL(POST_URL+token);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
if (msCookieManager != null) {
List<HttpCookie> cookies = msCookieManager.getCookieStore().getCookies();
if (cookies != null) {
if (cookies.size() > 0) {
for (HttpCookie cookie : cookies) {
String realCookie = cookie.toString().substring(cookie.toString().indexOf("=")+1);
}
con.setRequestProperty("Cookie", StringUtils.join(cookies, ";"));
}
}
}
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
if(response.indexOf("form-login")>0){
System.out.println("NOPE");
}
if(response.indexOf("logout")>0){
System.out.println("YES");
}
} else {
System.out.println("POST request not worked");
}
}
private static void jsoup(String cookie,String token) throws IOException{
Document response = Jsoup.connect("https://de.metin2.gameforge.com/")
.referrer("https://de.metin2.gameforge.com/main/index?__token=5"+token)
.cookie("Cookie","gf-locale=de_DE; _ga=GA1.2.404625572.1501231885; __auc=a02de56115d8864ad2bd7ad8120; pc_idt=ANu-cNegKMzU8CAsBQAHIu1cRlXEEUD1ka6NvJXWqO4sVVaLIsqSFPyZFt8dXHKcrhrB_u2FFdQnD-2vsA377NnVgjkrKn-3qzi5Q3LXbzgnbbmIEir4zYNCddPbjCUg9cVpSU4GP-CvU53XhrQ6_MWP9tOYNdjCqRVIPw; SID="+cookie+"; __utma=96667401.404625572.1501231885.1503225538.1503232069.15; __utmc=96667401; __utmz=96667401.1501247623.3.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)")
//.data("X-DevTools-Emulate-Network-Conditions-Client-Id","c9bbb769-df80-47ff-8e25-e582de026ecc")
.userAgent("Mozilla")
.data("username", "USERNAME")
.data("password", "PASSWORD")
.post();
System.out.println(response);
}
}
}
The var1 and var1 are unnecessary.
They was my first idea to send with my POST.
Here is a picture if the login-form:
And of HttpClient

org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

I'm trying to access another server for uploading files with Java. I'm able to connect to the server but I'm unable to upload the file.
The server (Spring Boot application) accepts the file in this way.
#PostMapping(value = ("multipart-store"), headers = ("content-type=multipart/*"))
CustomResponse gridFs(#RequestPart("file") MultipartFile multipartFile) throws Exception {
return new CustomResponse(storageService.storeObject(multipartFile));
}
I had tried couple of ways to access the server.
My Java code to access the server is below.
1st way
try {
final File uploadFile2 = new File("/home/thrymr/Desktop/invoicesample.pdf");
final String requestURL = "http://localhost:8082/data/multipart-store";
final MultipartUtility multipart = new MultipartUtility(requestURL, charset);
multipart.addFilePart("file", uploadFile2);
final List<String> response = multipart.finish();
System.out.println("SERVER REPLIED:");
for (final String line : response) {
System.out.println(line);
}
} catch (final IOException ex) {
System.err.println(ex);
}
public class MultipartUtility {
private final String boundary;
private static final String LINE_FEED = "\r\n";
private final HttpURLConnection httpConn;
private final String charset;
private final OutputStream outputStream;
private final PrintWriter writer;
public MultipartUtility(final String requestURL, final String charset) throws IOException {
this.charset = charset;
this.boundary = "" + System.currentTimeMillis() + "";
final URL url = new URL(requestURL);
this.httpConn = (HttpURLConnection) url.openConnection();
this.httpConn.setUseCaches(false);
this.httpConn.setDoOutput(true); // indicates POST method
this.httpConn.setDoInput(true);
this.httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + this.boundary);
this.outputStream = this.httpConn.getOutputStream();
this.writer = new PrintWriter(new OutputStreamWriter(this.outputStream, charset), true);
}
public void addFilePart(final String fieldName, final File uploadFile) throws IOException {
final String fileName = uploadFile.getName();
this.writer.append("file : "+uploadFile);
this.writer.flush();
final FileInputStream inputStream = new FileInputStream(uploadFile);
final byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
this.outputStream.write(buffer, 0, bytesRead);
}
this.outputStream.flush();
inputStream.close();
this.writer.append(LINE_FEED);
this.writer.flush();
}
public List<String> finish() throws IOException {
final List<String> response = new ArrayList<String>();
this.writer.append(LINE_FEED).flush();
this.writer.append("--" + this.boundary + "--").append(LINE_FEED);
this.writer.close();
final int status = this.httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(this.httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
this.httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
return response;
}
}
2nd way
final File file = new File("/home/thrymr/Desktop/invoicesample.pdf");
final HttpPost post = new HttpPost("http://localhost:8082/data/multipart-store");
final FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("file", fileBody);
final HttpEntity entity = builder.build();
post.setHeader("Content-Type", "multipart/form-data; boundary=gv");
final CloseableHttpClient client = HttpClients.createDefault();
post.setEntity(entity);
final HttpResponse response = client.execute(post);
System.out.println(response);
In the above addFilePart() method, by replacing this.writer.append("file : "+uploadFile); with below lines of code, i'm able to solve the issue.
this.writer.append("--" + this.boundary).append(LINE_FEED);
this.writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"")
.append(LINE_FEED);
this.writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
this.writer.append(LINE_FEED);

Obtaining the location of the redirected uri

This is what my URL look like:
https://localhost.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/
Upon posting that in java (http post request), it'll be re-directed to this URI:
https://kuma.ruto/v1/0auth/grant?state=some&code=YrnYdnHdY
How can I obtain the re-directed URI and get the code value?
Below is the code snippet:
String data = "email=p#f.com&password=Airtel#2017";
URL url = new URL("https://login.something.com/v1/oauth/authorize?response_type=code&state=none&email=p#f.com&client_id=jkhsdhaskfhdash&password=Airtel#2017&redirect_uri=https://login.something.com&code=none");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(
con.getOutputStream());
writer.write(data);
InputStream is = con.getInputStream();
System.out.println( "redirected url: " + con.getURL() );
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
System.out.println( "redirected url2: " + con.getURL() );
String st = null;
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
st += output;
}
Spdfast111 test = new Spdfast111();
Map<String, String> map=test.getFormParams(st);
URL url1 = new URL("https://login.something.com/v1/oauth/grant?value_one="+map.get("value_one")+"&value_two="+map.get("value_two")+"redirected_uri=https://login.something.com/v1/oauth/token");
System.out.println("==========================="+url1);
HttpURLConnection con1 = (HttpURLConnection) url1.openConnection();
System.out.println( "orignal url: " + con1.getURL() );
con1.connect();
System.out.println( "connected url: " + con1.getURL() );
InputStream is1 = connn.getInputStream();
System.out.println( "redirected url: " + con1.getURL() );
is1.close();
You can take a look at the following example:
https://github.com/mconf/bbb-java/blob/master/src/main/java/org/mconf/bbb/api/JoinServiceBase.java#L151
HttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet(joinUrl);
HttpContext context = new BasicHttpContext();
HttpResponse httpResponse = client.execute(method, context);
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
log.debug("HTTP GET {} return {}", joinUrl, httpResponse.getStatusLine().getStatusCode());
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(
ExecutionContext.HTTP_REQUEST);
if (!currentReq.getURI().getPath().equals("/client/BigBlueButton.html")) {
log.warn("It was redirected to {} instead of /client/BigBlueButton.html: the server was branded" +
" and the HTML name was changed, or it's an error. However, it will continue processing", currentReq.getURI().getPath());
}
HttpHost currentHost = (HttpHost) context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
String enterUrl = currentHost.toURI() + "/bigbluebutton/api/enter";
How about this example?:
// loop until no more redirections are
for (;;) {
if (Thread.currentThread().isInterrupted()) {
return null;
}
if(proxy != null) {
ucn = downloadURL.openConnection(proxy);
} else {
ucn = downloadURL.openConnection();
}
HttpURLConnection hucn = doConfigureURLConnection(ucn);
if(Thread.currentThread().isInterrupted())
return null;
ucn.connect();
int rc = hucn.getResponseCode();
boolean isRedirect =
rc == HttpURLConnection.HTTP_MOVED_TEMP ||
rc == HttpURLConnection.HTTP_MOVED_PERM;
if (!isRedirect) {
break;
}
String addr = hucn.getHeaderField(HTTP_REDIRECT_LOCATION);
URL newURL = new URL(addr);
if (!downloadURL.getProtocol().equalsIgnoreCase(newURL.getProtocol())) {
throw new ResourceRedirectException(newURL);
}
downloadURL = newURL;
}
Full code here: http://code.openhub.net/file?fid=eLvMdX5b01HDEtWkU2TEPg13uu0&cid=xJ52R73llJU&s=Obtaining%20the%20location%20of%20the%20redirected%20uri&pp=0&fl=Java&ff=1&filterChecked=true&fp=1141&mp,=1&ml=1&me=1&md=1&projSelected=true#L0

Delete issue in JIRA using Java

I am trying to write a method to delete an issue in JIRA.
I already have methods to create and update issues, but I cannot find any documentation on how to delete an issue using Java.
How can I delete a JIRA issue from a Java application?
You can try delete from IssueService: https://docs.atlassian.com/jira/latest/com/atlassian/jira/bc/issue/DefaultIssueService.html#delete(com.atlassian.crowd.embedded.api.User, com.atlassian.jira.bc.issue.IssueService.DeleteValidationResult)
You can try this:(here in this class I am getting specific user created issues by using JQL and after getting the response, I am deleting all of them one by one)
public class DeleteJiraIssuesHelper {
private InputStream inputStream;
private JsonReader jsonReader;
public void deleteJiraIssues() throws JiraDeleteIssueException, IOException {
String JQL="your jql query";
try {
URL url = new URL(
"your jira url/rest/api/latest/search?jql="+JQL);
String userpass = "UserName" + ":" + "Password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", basicAuth);
conn.setRequestProperty("Content-Type", "application/json");
if (conn.getResponseCode() != 200) {
throw new JiraConnectionException("Failed : HTTP error code : " + conn.getResponseCode());
}
conn.getResponseMessage();
inputStream = conn.getInputStream();
jsonReader = Json.createReader(inputStream);
JsonObject jsonObject = jsonReader.readObject();
int no = jsonObject.getInt("total");
JsonArray jsonArray = jsonObject.getJsonArray("issues");
List<String> issueList = new ArrayList<>();
for (int i = 0; i <= no - 1; i++) {
JsonObject jsonObject2 = jsonArray.getJsonObject(i);
String key = jsonObject2.getString("key");
issueList.add(key);
}
conn.disconnect();
HttpURLConnection httpCon = null;
for (int i = 0; i < no; i++) {
URL url2 = new URL(
"Jira url/rest/api/latest/issue/" + issueList.get(i));
httpCon = (HttpURLConnection) url2.openConnection();
httpCon.setRequestProperty("Authorization", basicAuth);
httpCon.setDoOutput(true);
httpCon.setRequestProperty("Content-Type", "application/json");
httpCon.setRequestMethod("DELETE");
httpCon.connect();
httpCon.getResponseCode();
httpCon.disconnect();
}
} catch (IOException ex) {
throw new JiraDeleteIssueException("MalformedURLException: " + ex);
} finally {
inputStream.close();
jsonReader.close();
}
}
}

Uploading Multipart-form-data to php

I would like upload any data (Username, Description,..) and a photo from my Android-App to my Server (PHP-Script).
By searching for this i found a solution: How to take a photo and send to HTTP POST request with Android?
By click on my send button, the upload-process starts and the php-script works but the $_POST -Vars are empty? Can anybody help me? My request-code:
[...]
case R.id.btnSend:
String strName = tName.getText().toString();
String strEmail = tEmail.getText().toString();
String strDatum = tDatum.getText().toString();
String strBeschreibung = tBeschreibung.getText().toString();
nvPairs = new ArrayList<NameValuePair>(2);
nvPairs.add(new BasicNameValuePair("sender", "AndoidApp"));
nvPairs.add(new BasicNameValuePair("name", strName));
nvPairs.add(new BasicNameValuePair("email", strEmail));
nvPairs.add(new BasicNameValuePair("datum", strDatum));
nvPairs.add(new BasicNameValuePair("beschreibung", strBeschreibung));
nvPairs.add(new BasicNameValuePair("bild", bmpUrl));
new UploadTask().execute();
}
[...]
private class UploadTask extends AsyncTask<Void, Void, List<String>> {
#Override
protected List<String> doInBackground(Void... params) {
String response = "";
try {
response = new MultipartServer().postData(new URL(postUrl), nvPairs);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
On the MultipartServer-Code i changed the line:
if ((avatarName = pair.getName()).equals("avatar")) {
avatarPath = pair.getValue();
}
to this:
if ((avatarName = pair.getName()).equals("bild")) {
avatarPath = pair.getValue();
}
And the Variables "bmpUrl" and "postUrl" are set correctly at an another line in my code.
Thanks for your help! I found an solution for the problem. The problem was at the Script from How to take a photo and send to HTTP POST request with Android?
I changed it to this:
public class MultipartServer {
private static final String TAG = "MultipartServer";
private static final String crlf = "\r\n";
private static final String twoHyphens = "--";
private static final String boundary = "AaB03x"; //*****
private String avatarName = null;
private String avatarPath = null;
public String postData(URL url, List<NameValuePair> nameValuePairs) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
FileInputStream inputStream;
OutputStream outputStream = connection.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
for (NameValuePair pair : nameValuePairs) {
dataOutputStream.writeBytes(crlf);
dataOutputStream.writeBytes(twoHyphens + boundary + crlf);
dataOutputStream.writeBytes(
"Content-Disposition: form-data; name=\""
+ URLEncoder.encode(pair.getName(), "UTF-8")
+ "\";" + crlf);
dataOutputStream.writeBytes(crlf);
dataOutputStream.writeBytes(URLEncoder.encode(pair.getValue(), "UTF-8"));
if (pair.getName().equals("bild")) {
avatarName = pair.getName();
avatarPath = pair.getValue();
}
}
// Write Avatar (if any)
if (avatarName != null && avatarPath != null) {
dataOutputStream.writeBytes(crlf);
dataOutputStream.writeBytes(twoHyphens + boundary + crlf);
dataOutputStream
.writeBytes("Content-Disposition: form-data; name=\""
+ avatarName + "\";filename=\""
+ new File(avatarPath).getName() + "\";" + crlf);
dataOutputStream.writeBytes(crlf);
inputStream = new FileInputStream(avatarPath);
byte[] data = new byte[1024];
int read;
while ((read = inputStream.read(data)) != -1)
dataOutputStream.write(data, 0, read);
inputStream.close();
dataOutputStream.writeBytes(crlf);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
}
dataOutputStream.flush();
dataOutputStream.close();
String responseMessage = connection.getResponseMessage();
Log.d(TAG, responseMessage);
InputStream in = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(in, "UTF-8"));
StringBuilder response = new StringBuilder();
char[] b = new char[512];
int read;
while ((read = bufferedReader.read(b)) != -1) {
response.append(b, 0, read);
}
connection.disconnect();
Log.d(TAG, response.toString());
return response.toString();
}
}

Categories

Resources