GithubHelp home page GithubHelp logo

seif-apis / javafileuploader Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 61 KB

Simple JAVA API to upload files using PHP scripts and HTTP requests

java file-upload apache wamp xamp php http http-server http-request java-files

javafileuploader's Introduction

JavaFileUploader V1.0

Server configuration:

  1. Create a folder inside your server (www for wamp).
  2. Extract scripts.rar archive.
  3. Copy the two php files (upload.php and delete.php) inside the folder you created earlier.
  4. Done.

Usage:

  • The JAR file provided should be included as a library in your JAVA project

  • You need to provide the URL to the folder you created earlier in you server to the JavaFileUploader JAVA API as serverURL in the constructor.
    (exemple: "localhost/foldername")

  • Your uploaded files will be under a folder named "uploads" under the folder you created earlier.

  • To get an uploaded file you need the URL to the "uploads" folder and the file name as stored in the server.
    (exemple: http://localhost/foldername/uploads/filename.extension)

  • PS: File name as stored in server is the return value of the upload method in the JavaFileUploader API


Server folder tree exemple (wamp):

├── www
│   ├── exmplefolder
│   │   ├── upload.php
│   │   ├── delete.php
│   │   ├── uploads
│   │   │   ├── f_5ac66af018455.jpg
│   │   │   ├── f_c4zd625c12785.pdf
│   │   │   ├── f_t4ce8a4135btw.txt

URLs for this tree will be:

Valid ServerUrls:

Files URL:


Code snippets

Upload & Delete

    // ...
    import java.io.IOException;
    import java.net.ProtocolException;
    import rest.file.uploader.tn.FileUploader;
    // ...
    
    public static void main(String[] args) throws ProtocolException, IOException{
        // ...
        FileUploader fu = new FileUploader("localhost/exemplefolder");
        
        //Upload
        String fileNameInServer = fu.upload("C:/Users/Public/test.pdf");
        
        // ...
        
        //Delete
        if(fu.delete(fileNameInServer)){
            System.out.println("File "+fileNameInServer+" deleted.");
        }
        
        // ...
    }

Get file extension

    // ...
    System.out.println(FileUploader.getFileExtension("C:/Users/Public/test.png")); /*This will print "png"*/
    // ...

Usage with Symfony

To make this uploader compatible with Symfony, place the scripts inside your project web folder, the scripts will then create a folder named "Uploads", all uploaded files will be saved inside that folder. To change the upload destination folder, open the upload.php script and change the folder path (do the same for delete.php script).

The upload method will return a string consisting of the file name as stored in the service, concat it with your server web folder url to get the url to the uploaded image.

Exemple

http://localhost/[PROJECT_NAME]/web/uploads/[FILE_NAME]

Code snippets

    // ...
    import rest.file.uploader.tn.FileUploader;
    // ...
    
    try{
        FileUploader fu = new FileUploader("localhost/[PROJECT_NAME]/web/");
        
        //Upload
        String fileNameInServer = fu.upload("[FILE_PATH]");
        
        //Delete
        if(fu.delete(fileNameInServer)){
            System.out.println("File "+fileNameInServer+" deleted.");
        }
    }catch(Exception ex){
        ex.printStackTrace();
    }
    
    // ...

Potential errors:

  • Try changing your server limits (post_max_size, upload_max_filesize, memory_limit) to -1 in the server's php.ini file.

javafileuploader's People

Contributors

seifabdennadher avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

omar-adel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.