2016-03-16 85 views
1

我想上傳image文件在服務器上,因爲沒有找到任何方式直接從CodenameOne上的服務器上傳文件。我以Base64格式轉換圖像併發送到php文件,然後在php再次解碼圖像並嘗試上傳。但同樣也不起作用。你能告訴我我該如何解決這個問題?通過PHP在服務器上傳文件[CodenameOne]

我的PHP代碼:

<?php 

define('UPLOAD_DIR', 'public_html/appFiles/GKQuiz/userProfilePicture/'); 
//$Imagecode=$_POST['Image']; 


//Base64 String already added in $Imagecode 
$Imagecode="/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABAAEADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09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SCFBH4bh0/wA9eat/8JGXbc9lGdpyBHI6hvrknP8An61zzMCQ6oUGOhPX9PSgMUG3GP8AdIz9atTktmQ4piJxjcRkdD6Uu4rhmJJxtyTjt0xTTlifkOM4yOSMf/WpYbeSbaiEhXbGSOOlTexSV9hyyRliXAOO2euOlXBbzWQWVANyqDyOmcj8Dz2qSytbeNA7yp5/AAyQF5zuyO449e/oM22t/tjeY7PsG1c5w20A5wMjJ6DNYyq2fkbRpXXmZsTxxCUM6uzpnGM4+h9ec+nHrUNxDLFKEVzIwAJw3cjPH+NT3FtMYGvImxAcLgvlj6ZHfp9Pl9qqCdo4niJJJYZ59M/4mtE77GTVtx5uY1EexPLdBgkHduOTyCenYfhmpYxHJIFEgaPJPGQW9BgDOTjjsMj3quwWVUJUbjxwep9sf1pjQyKgbDFV49h1/wD11VhXP//Z"; 

$img=base64_decode($Imagecode); 
//echo $img; 
$uid=uniqid(); 

$ftp_server = "example.com"; 
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to ftp_server"); 
$login = ftp_login($ftp_conn, "example.com", "123"); 

if (ftp_put($ftp_conn, UPLOAD_DIR . $uid . ".jpg", $img, FTP_BINARY)) 
{ 
    echo "Successfully uploaded"; 
} 
else{ 
    echo "Error uploading"; 
} 

// close connection 
ftp_close($ftp_conn); 

?> 

回答

0

最後找到與下面相同的答案。

修正PHP代碼:

<?php 

define('UPLOAD_DIR',$_SERVER['DOCUMENT_ROOT'].'/appFiles/GKQuiz/userProfilePicture/'); 

// after $_SERVER['DOCUMENT_ROOT']. your root inside public_html folder if any or '/'. 

$Imagecode=$_POST['Image']; // parameter 

$img=base64_decode($Imagecode); 
$uid=uniqid(); 

$file = UPLOAD_DIR . $uid . '.jpg'; 
$success = file_put_contents($file, $img); 

if ($success) 
{ 
    echo $uid.'.jpg'; 
} 
else 
{ 
    echo "error"; 
} 

?> 

在CodenameOne你的代碼可能如下:

if (profilepic!=null) { 
ImageIO imgIO = ImageIO.getImageIO(); 
ByteArrayOutputStream out = new ByteArrayOutputStream(); 
imgIO.save(profilepic, out, ImageIO.FORMAT_JPEG, 1); 
byte[] ba = out.toByteArray(); 
String Imagecode = Base64.encode(ba); 
//System.out.println("data :"+Imagecode); 
request = new ConnectionRequest(){ 
    @Override 
    protected void handleErrorResponseCode(int code, String message) { 
      System.out.println("Code :"+code+" Msg :"+message); 
    } 

}; 
request.setPost(true); 
request.setHttpMethod("POST"); 
request.addArgument("Image", Imagecode); 
request.setUrl("http://example.com/filename.php"); 
NetworkManager.getInstance().addToQueueAndWait(request); 
}else{ 
    System.out.println("Unable to upload"); 
} 
0

Codename One有一個內置的標準上傳API通在MultipartRequest多部分請求HTTP標準的支持。

MultipartRequest無縫地處理二進制的base64等

MultipartRequest request = new MultipartRequest(); 
request.setUrl(url); 
request.addData("myFileName", fullPathToFile, "text/plain") 
NetworkManager.getInstance().addToQueue(request); 

的Java servlet代碼來攔截這應該看起來像:

@WebServlet(name = "UploadServlet", urlPatterns = {"/upload"}) 
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 100, // 10 MB 
     maxFileSize = 1024 * 1024 * 150, // 50 MB 
     maxRequestSize = 1024 * 1024 * 200)  // 100 MB 
public class UploadServlet extends HttpServlet { 

    @Override 
    public void doPost(HttpServletRequest req, HttpServletResponse res) 
      throws ServletException, IOException { 
     Collection<Part> parts = req.getParts(); 
     Part data = parts.iterator().next(); 
     try(InputStream is = data.getInputStream()) {} 
      // store or do something with the input stream 
     } 
    } 
} 

我不是一個PHP的人,但一個快速谷歌搜索登陸我在w3c school與此

<?php 
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 
?> 
相關問題