将文件从java上传到php服务器并保存在mysql数据库中

时间:2013-04-20 06:42:48

标签: java php mysql

这是java应用程序中的代码

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 a buffer of maximum size

                        int bytesAvailable = fis.available();
                        int maxBufferSize = 1024;
                        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
                        byte[] buffer = new byte[bufferSize];

                        // read file and write it into 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);

这是php代码服务器端。输出始终为null。

<?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 INTO 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";
}

?>

我总是从php获取null意味着该行未插入数据库。 有什么问题?

0 个答案:

没有答案