2016-04-03 10 views
1

çalışmıyor Ben Java aşağıdaki yükleyici yöntemi:Dosyayı yükleme

public void uploadFile() throws IOException 
{ 
HttpClient httpclient = new DefaultHttpClient(); 
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
HttpPost httppost = new HttpPost("http://myurl/uploadper.php"); 
File file = new File("C:/Users/mislam/Desktop/perfimg/0.jpg"); 
MultipartEntity mpEntity = new MultipartEntity(); 
ContentBody cbFile = new FileBody(file, "image/jpeg"); 
mpEntity.addPart("userfile", cbFile); 
httppost.setEntity(mpEntity); 
System.out.println("executing request " + httppost.getRequestLine()); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity resEntity = response.getEntity(); 

System.out.println(response.getStatusLine()); 
if (resEntity != null) { 
    System.out.println(EntityUtils.toString(resEntity)); 
} 
if (resEntity != null) { 
    resEntity.consumeContent(); 
} 

httpclient.getConnectionManager().shutdown(); 
} 

Ben aşağıdaki çıktıyı

alıyorum

<?php 
$target_path = "uploads/"; 

$target_path = $target_path . basename($_FILES['userfile']['name']); 

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['userfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 
?> 

aşağıdaki php dosyası

executing request POST http://reactor.ctre.iastate.edu/uploadper.php 

HTTP/1.1 
HTTP/1.1 200 OK 
There was an error uploading the file, please try again! 

Sunucuyla iletişimin başarılı olduğunu ancak yüklenemediğini gösterir. PHP'nin dosyayı yüklememesinin nedeni ne olabilir? Java kodunda herhangi bir sorun var mı?

DÜZENLEME Sunucu Ama yüklemeler operasyonda dosyayı almak çok parçalı varlık için kod Yeniden Yazma sonra çalışmıyor:

HttpEntity httpEntity = MultipartEntityBuilder.create() 
    .addBinaryBody("userfile", file, ContentType.create("image/jpeg"), file.getName()) 
    .build(); 
    httppost.setEntity(httpEntity); 
+2

print_r ile başlayın ($ _ FILES ['userfile']) ' –

+0

Hiçbir şey göstermiyor. Nedenini merak ediyorum!! –

cevap

1

Önce $ _FILES değerinde bir göz olurdu [ 'kullanici_dosyasi '] [' error ']

hata kodlarının açıklaması için bu yardım sayfasına bakabilirsiniz:

http://php.net/manual/en/features.file-upload.errors.php

+0

Hiçbir şey göstermiyor. Nedenini merak ediyorum!! –

+0

İşlenmemiş http isteği alabilir misiniz? Bozuk olabilir. Kontrol edilecek bir şey içerik tipi başlıktır. Doğru değilse php dosyayı görmez. Olması gereken: Content-Type: multipart/form-data –

+0

Gösterilen Content-Type: multipart/form-data; Sınır = YjiAoTjQY6yMFjEStp8ryIbHHeF_9BPAova4d3Jb; charset = US-ASCII –