uploading files from java to php server and saving in mysql database -
this code in java application
url url = new url("http://gguproject.jelastic.servint.net/upload.php"); string filename="c:/square.png"; fileinputstream fis=new fileinputstream(filename); httpurlconnection hp=(httpurlconnection)url.openconnection(); hp.setdoinput(true); hp.setdooutput(true); hp.setinstancefollowredirects(false); hp.setrequestmethod("post"); hp.setrequestproperty("connection", "keep-alive"); string boundary = "*****"; string lineend = "\r\n"; string twohyphens = "--"; hp.setrequestproperty("content-type", "multipart/form-data;boundary="+boundary); dataoutputstream dos = new dataoutputstream( hp.getoutputstream() ); dos.writebytes(twohyphens + boundary + lineend); dos.writebytes("content-disposition: form-data; name=\"uploadedfile\";filename=\"" + filename +"\"" + lineend); dos.writebytes(lineend); // create buffer of maximum size int bytesavailable = fis.available(); int maxbuffersize = 1024; int buffersize = math.min(bytesavailable, maxbuffersize); byte[] buffer = new byte[buffersize]; // read file , write form... int bytesread = fis.read(buffer, 0, buffersize); while (bytesread > 0) { dos.write(buffer, 0, buffersize); bytesavailable = fis.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fis.read(buffer, 0, buffersize); } // send multipart form data necesssary after file data... dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + twohyphens + lineend); fis.close(); dos.flush(); dos.close(); string line; bufferedreader reader = new bufferedreader(new inputstreamreader(hp.getinputstream())); line = reader.readline(); system.out.println(line);
this php code server side. output null always.
<?php include('dbc.php'); $filename = $_files['userfile']['name']; $tmpname = $_files['userfile']['tmp_name']; $filesize = $_files['userfile']['size']; $filetype = $_files['userfile']['type']; $email="sangwan.ritesh@yahoo.in"; $hash="sdfhghgsdfhguidutetuhgdfjgdfhslgjhdfjgh"; $fp = fopen($tmpname, 'r'); $content = fread($fp, filesize($tmpname)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $filename = addslashes($filename); } $query = "insert files (email, file_name, file_ext, file_size, file_hash,file_data ) values ('$email','$filename', '$filetype', '$filesize','$hash', '$content')"; mysqli_query($dbc,$query); if(mysqli_affected_rows($dbc)==1){ echo "successfully uploaded"; } ?>
i getting null php means row not being inserted database. problem ?
Comments
Post a Comment