Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6939813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:40:16+00:00 2026-05-27T12:40:16+00:00

Here with another question on Images ( which seems to be more difficult than

  • 0

Here with another question on Images ( which seems to be more difficult than I initialy predicted) I’m working on a java Web app with JSF 2.0 ( apache myFaces) and I want this app to be able to upload a picture to a destination on the server it’s going to run on. I have a Windows r2 2008 Server running a mySQL Db, but I don’t want to store the image in the db, I’d rather store it somewhere in the server and then just save the path as a string in the db.

I was told this is the best way, but I can’t seem to find an example on how to save it on the server. I run the app on the Apache tomcat Server as a WAR file. so I don’t know if I have to save the file to a path on the server drive (i.e. C:\images) or a special folder in the project itself ( within the java, html files) any help at all is greatly appreciated. I’m totally lost and have been stuck the whole day trying to figure this out.

The code I use to upload the image to the java class is this ( courtesy of CodyS):

InputStream is = uploadedFile.getInputStream();  
byte[] buffer = new byte[(int) uploadedFile.getSize()];
is.read(buffer); 
File f = new File("C:\\temp\\" + this.patient.getPk() + ".jpeg");   
f.createNewFile();  
FileOutputStream fos = new FileOutputStream(f);  
fos.write(buffer);  //This is where I write it to the C Drive
fos.close();
is.close(); 

instead of writing it to my C drive I’m going to run it on the server, but where should I store the image to later retriev and display in an xhtml file? I hope I’m being clear on what I need, let me know if I am not and I’ll try to explain in another way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:40:16+00:00Added an answer on May 27, 2026 at 12:40 pm

    instead of writing it to my C drive I’m going to run it on the server, but where should I store the image to later retriev and display in an xhtml file?

    That depends on how much control you have over configuring the server. Ideal would be to configure a fixed path outside the Tomcat webapps folder. For example, /var/webapp/upload. You can set this path as a VM argument or environment variable so that your webapp can retrieve it programmatically without the need to change the code.

    For example, when specifying as VM argument -Dupload.location=/var/webapp/upload, you can complete the upload as follows:

    Path folder = Paths.get(System.getProperty("upload.location"));
    String filename = FilenameUtils.getBaseName(uploadedFile.getName()); 
    String extension = FilenameUtils.getExtension(uploadedFile.getName());
    Path file = Files.createTempFile(folder, filename + "-", "." + extension);
    
    try (InputStream input = uploadedFile.getInputStream()) {
        Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
    }
    
    String uploadedFileName = file.getFileName().toString();
    // Now store it in DB.
    

    As to serving the file back, most ideal would be to add the upload location as a separate <Context> to Tomcat. E.g.

    <Context docBase="/var/webapp/upload" path="/uploads" />
    

    This way you can access it directly by http://example.com/uploads/foo-123456.ext

    If you have zero control over configuring the server, then, well, storing in the DB or sending to a 3rd party host such as Amazon S3 is your best bet.

    See also:

    • How to provide relative path in File class to upload any file?
    • Reliable data serving
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's another C#/.NET question based merely on curiousity more than an immediate need ...
Hi Masters Of Web Programming, here I come with another stupid question, hoping someone
yet another PHP question which I am stuck on, so here is what im
Well, creating thumbnail images in an asp.net app seems more painful then I ever
While reading through another question here, on creating a URL shortening service, it was
In answering another persons question here on SO, I discovered that there is a
I am currently making an app which works with images. I need to implement
Here is a link to another question I asked concerning the same project I
Here's another one of these LinqToSQL questions where I'm sure I must have missed
I have an other active question HERE regarding some hopeless memory issues that possibly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.