Empty Request.FILES when uploading file from android client - java

i try upload file from android client to Django view
but on uploading that request.FILES are always empty
here is my django view code:
views.py
def vw_reception_uploadimage(request, phonenumber):
if request.method == 'POST':
print request.META
try:
imagePath = '/home/user/Pictures/' + str(int(time.time() * 1000)) + '.jpg'
destination = open(imagePath, 'wb+')
for chunk in request.FILES["uploadedfile"].chunks():
destination.write(chunk)
destination.close()
except Exception as e:
print e
print request.FILES
return HttpResponse("ok")
and here is my server file upload AsyncTask:
class ServerFileUploadTask extends AsyncTask {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "----*****";
private Activity activity;
private String filepath;
ServerFileUploadTask(Activity activity,String filepath)
{
this.activity=activity;
this.filepath=filepath;
}
#Override
protected Void doInBackground(String... uri) {
long length = 0;
int progress;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 2048 * 1024;// 256KB
try {
FileInputStream fileInputStream = new FileInputStream(new File(
filepath));
File uploadFile = new File(this.filepath);
long totalSize = uploadFile.length(); // Get size of file, bytes
URL url = new URL(uri[0]);
connection = (HttpURLConnection) url.openConnection();
// Set size of every block for post
connection.setChunkedStreamingMode(2048 * 1024);// 256KB
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
if(PrefSingleton.getInstance().readPreference("token", null)!=null){
connection.setRequestProperty("AUTHORIZATION" , "Token "+PrefSingleton.getInstance().readPreference("token", null));
}
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
outputStream = new DataOutputStream(
connection.getOutputStream());
outputStream
.writeBytes(twoHyphens + boundary + lineEnd+"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
+ this.filepath + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
length += bufferSize;
progress = (int) ((length * 100) / totalSize);
publishProgress(progress);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
}
return null;
}
#Override
protected void onPostExecute(Void result) {
}
}
i tried most examples with no success and most of them use methods and class that are deprecated
i using Django version 1.7 and development server on port 8080
also using android version 4.2
there is some other examples that use httpentity and http entity builder
that don't work for me

i can't find solution with above code but ended up solving problem by using
Android Asynchronous Http Client library
http://loopj.com/android-async-http/

Related

How to upload a video file via HTTP POST in JAVA without using youtube api

I want to upload some video file to my Youtube.
I know the official site (https://developers.google.com/youtube/v3/docs/videos) but I'm using android platform but there is no android play services.
And then I know about Google API Client Library.
I look up API samples
(https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/UploadVideo.java).
The sample use com.google.api.services.youtube. but i can't use the YouTube APIs.
I just upload a video file via http in java without youtube api.
This is my test source.
Annotation (// this is not working) is sending vedio. If the source Uncommented, I received 400 responses. If the source Comment, I received 200 responses.
public static String uploadMetaData(String filePath, boolean retry, String accessToken) throws IOException {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
//String baseURL = "https://www.googleapis.com/youtube/v3/videos";
String baseURL = "https://www.googleapis.com/upload/youtube/v3/videos";
String urlData = "uploadType=resumable&part=snippet,status,contentDetails";
String uploadUrl = baseURL + "?" + urlData;
String postDataForm = "{\n\"snippet\": {\n\"title\": \"%s\",\n\"description\": \"%s\",\n\"tags\": " + "[\"cool\", \"video\", \"more keywords\"],\n\"categoryId\": 22\n},\n\"status\": " + "{\n\"privacyStatus\": \"public\",\n\"embeddable\": True,\n\"license\": \"youtube\"\n}\n}";
String postData = String.format(postDataForm, "hong", "hong", "hong");
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(postData);
} catch (JSONException e) {
e.printStackTrace();
}
URL url = new URL(uploadUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Host", "www.googleapis.com");
urlConnection.setRequestProperty("Authorization", String.format("Bearer %s", accessToken));
urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
urlConnection.setRequestProperty("X-Upload-Content-Type", "video/*");
//urlConnection.setRequestProperty("Content-Length", String.valueOf(postData.length()));
//urlConnection.setRequestProperty("X-Upload-Content-Length", );
urlConnection.setDoOutput(true);
String uploadFileName = "1541437905497.mp4";
String fileName = Environment.getExternalStorageDirectory().getPath() + "/cmxMediaGallery/1541437905497.mp4";
FileInputStream fileInputStream = new FileInputStream(fileName);
DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());
dos.writeBytes(jsonObj.toString());
// this is not working
/*dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=" + uploadFileName + ";filename="
+ fileName + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}*/
dos.flush();
dos.close();
int responseCode = urlConnection.getResponseCode();
if (responseCode < 200 || responseCode >= 300) {
if ((responseCode + "").startsWith("4") && retry) {
return uploadMetaData(filePath, false, accessToken);
} else {
throw new IOException(String.format("response code='%s' (code %d)" + " for %s",
urlConnection.getResponseMessage(), responseCode, urlConnection.getURL()));
}
}
Log.d("Hong", "responseCode : " + responseCode);
return urlConnection.getHeaderField("Location");
}
Some example use multipart. but it's working.
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
I wanna know how to upload a video file via HTTP POST in JAVA without using youtube api.

trying to upload a image to my server from android

am using this code to upload an image to my web server:
public void send (View view)
{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "sdcard/yo.jpg";
String urlServer = "http://192.168.1.4/uplaod.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
Log.i("ODPOWIEDZ", serverResponseMessage);
Context context = getApplicationContext();
Toast.makeText(context, "its here 1 ", Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Context context = getApplicationContext();
Toast.makeText(context, "its in catsh", Toast.LENGTH_LONG).show();
Log.i("WYJATEK", ex.getMessage());
}
}
and i get this message from my LogCat where is the problem here ??
59-124/system_process D/SntpClient﹕ request time failed: java.net.SocketException: Address family not supported by protocol
this is my php file
<?php
$target_path = "uploadedfile/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
}
else {
echo "There was an error uploading the file, please try again!";
}
?>
The SNTP error relates to the time protocol, which, incidentally has nothing to do with your app. It looks like it's just the emulator trying to fetch the latest timestamp off an ntp server. Just discard it.
The issue is still something else. You did notice that there might be a typo in the name of the php script? In your code you refer to it as uplaod.php.
Also, make sure you have enabled internet access in your manifest file:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Error upload image in Android app -- Api Foursquare

I am developing an Android application in which users can upload photos of venues you visit. I'm using the endpoints and have begun testing https://api.foursquare.com/v2/photos/PHOTO_ID through Apigee console. The problem I have is that if I send an image via the console returns me an error 400 problems with mime type as you can in this still image is correct, I think.
https://docs.google.com/file/d/0B9uUMZ3ZVbl_bG5rOUNhM01wb2M/edit?usp=sharing (console error)
I have also tried uploading through the application I am developing and I returned the same error. The execute method in the app is as follows:
TokenStore tokenStore=new TokenStore(getApplicationContext());
String token=tokenStore.getSavedToken();
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = params[0];
String urlServer = "https://api.foursquare.com/v2/photos/add?public=1&venueId=5252da418bbd79f3aaa70ae6&oauth_token="+token;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "image/jpeg");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
//outputStream.writeBytes("Content-Type: image/jpeg");
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.i("serverResponse", "serverresponse "+ serverResponseMessage);
Log.i("serverResponseCode", "serverResponseCode "+ serverResponseCode);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex) {
//Exception handling
}
In my tests online server, upload the image class works fine so the problem must be with some kind of data that does not put right for the Foursquare API. Anyone have experience with this? thanks

POSTing an image to PHP file

I'm trying to POST an image to a PHP file from my android app and am wondering what format (File, Fileoutputstream etc) I have to post it in for it to be recognized as a file and refer to it with $_FILE['filename'] in my php script.
Thanks :)
EDIT:
Sorry I may not have been clear, I'm not looking for the PHP script, I already have that finished accepting the $_FILE['sample'] and doing what I need with it, I'm just not sure the file TYPE that I have to post to the php file (IN JAVA) in order for php to 'see' it as $_FILE
FYI: I am using the loopj asynchronous http request library.
public void add_image_android(final Bitmap image, String party_id, String guest_id)
{
String url = "http://www.mysite.com/urltopost";
/* not sure what to set fOut to for the bitmap to be passed as file */
RequestParams params = new RequestParams();
params.put("file", fOut);
params.put("guest_id", guest_id);
params.put("party_id", party_id);
client.post(url, params, new JsonHttpResponseHandler()
{
#Override
public void onSuccess(JSONObject response)
{
((ResponseListener)_mainContext).add_image_android_response(response.toString());
return;
}
#Override
public void onFailure(Throwable e)
{
fireToast("api error:"+e);
Log.d("api error:",e.toString());
}
});
}
Try the Below Code which will upload the Image and Give the Link.
<?php
$uploaddir = 'images/';
$ran = rand () ;
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir .$ran.$file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "http://www.domain.com/folder/{$uploadfile}";
}
?>
This worked for me: (very old code, hope it helps...)
ReturnObject returnObject = new ReturnObject();
HttpURLConnection conn = null;
DataOutputStream dos = null;
BufferedReader inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = "your url";
try{
FileInputStream fileInputStream = new FileInputStream(photoFile);
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"image\";"
+ " filename=\"" + photoFile.getAbsolutePath() +"\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0){
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
dos.close();
}catch (MalformedURLException ex){
ex.printStackTrace();
}catch (IOException ioe){
ioe.printStackTrace();
}
On the server i found this:
$source = $_FILES['image']['tmp_name'];
move_uploaded_file($source, $target)
Not sure what this "tmp_name" is...

Uploading an image to a server in android

I my app I am trying to send an image over a PHP server. The problem is the PHP programmers say that it should be sent as an data. Following is the code which I am currently using, please help me to convert the image to a data and get a response from the server.
InputStream is;
private int serverResponseCode;
private String serverResponseMessage;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "/sdcard/siva.PNG";
Log.e("pathToOurFile",""+pathToOurFile);
String urlServer = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
Log.e("URL Server",""+urlServer);
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
Log.e("maxBufferSize",""+maxBufferSize);
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
Log.e("FIS",""+fileInputStream);
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
Log.e("con",String.valueOf(connection.getDoOutput()));
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
Log.e("response",""+serverResponseCode);
Log.e("serverResponseMessage",""+serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
Log.e("Exception Handling",""+ex);
}
}
Atlast i found the answer, i did a small mistake....In the above code i have changed only one word in the following line
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
In the above line the main part is the word "uploadedfile". This word must be specified by the php programmer or else the file which we are sending will not be replaced.
Please refer the here

Categories