0

I'm following this tutorial Upload and display image from MySQL db using PrimeFaces

It works and everything is fine, but what I really need is, to save the images in the server file manager, and it's path in the database.

I tried this method which only works only if there is a php file in the server to handle it. Which I can't use because of the requirement.

java:

//uploading the image to the web server
public void uploadImg(String path){
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;
    String pathToOurFile = path ;
    //host url
    String urlServer = "/UploadImage.php"; // here should be the url to the php file in the server
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;{

        try
        {
            FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs.
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Set HTTP method to POST.
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

            outputStream = new DataOutputStream( connection.getOutputStream() );
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
            outputStream.writeBytes("Content-Type: image/jpeg" + lineEnd);
            outputStream.writeBytes(lineEnd);

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

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
        }
        catch (Exception ex)
        {
            //Exception handling
        }}

    }

My question is how can I do the requested without using php? is there any other way? like html fie instead? or jsp?

sasuri
  • 972
  • 3
  • 11
  • 26
  • That blog/article/tutorial or whatever uses ``. No code like this is needed to upload files to the server's disk file system and save its name to the database. Only the library Apache Commons FileUpload and its dependency Apache Commons IO are needed on the classpath. The libraries in conjunction with the framework take care of all other dirty tasks under the hood. Just a few functions to read and write the file streams to and from the disk file system are needed. – Tiny Dec 19 '14 at 11:55
  • I was searching and the only thing I found was they upload the files to a local folder not to the server like http://www.javacodegeeks.com/2013/08/file-upload-example-in-servlet-and-jsp.html , do you have a tutorial or sth? @Tiny – sasuri Dec 19 '14 at 12:01
  • Files are actually uploaded to a fixed location on the server. They should not be uploaded to the deployed WAR file (as you probably mean by "*local folder*"), since the WAR file is not meant for permanent file storage and those uploaded files are lost, if the application is redeployed. http://stackoverflow.com/a/8889096/1391249, http://stackoverflow.com/a/19142316/1391249, http://stackoverflow.com/a/8521981/1391249 – Tiny Dec 19 '14 at 12:13
  • See [they](http://www.javacodegeeks.com/2013/08/file-upload-example-in-servlet-and-jsp.html) are also uploading files to a fixed location (fixed path) on the server - `private final String UPLOAD_DIRECTORY = "C:/uploads";` using the Apache Commons FileUpload library. – Tiny Dec 19 '14 at 12:17

0 Answers0