0

I am doing some java web applications using eclipse, and I host the .war file on glassfish4 server in other PC.

I have written a jsp page, the jsp page needs the user to enter all kind of information and browse any kind of file (such as txt, or image file) and upload to the glassfish4 local server.

I have done a lot of searching on Google, and I couldn't found any solution on that, can you all help me on these?

This is my JSP page code :

<form action="uploadFile" method="POST" enctype="multipart/form-data">
                            <div class="card-body">
                                <h5 class="card-title">Equipment Request</h5>
                                <div class="form-group row">
                                    <label for="reqEmail" class="col-sm-2 text-right control-label col-form-label">Requestor</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="reqEmail" name="reqEmail" disabled value="reggiezhiyi.lim@motorolasolutions.com">
                                    </div>
                                    <label for="ID" class="col-sm-1 text-right control-label col-form-label">Request ID</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="ID" name="ID" disabled value="0000001">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="projectName" class="col-sm-2 text-right control-label col-form-label">Project</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="projectName" name="projectName" maxlength="50" placeholder="Enter the Project Name..">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="contactNum" class="col-sm-2 text-right control-label col-form-label">Contact No.</label>
                                    <div class="col-sm-3">
                                        <input type="number" class="form-control" id="contactNum" name="contactNum" maxlength="50" placeholder="Enter the Phone Number">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="manID" class="col-sm-2 text-right control-label col-form-label">Manager Core ID</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="manID" name="manID" maxlength="50" placeholder="Enter the Manager Core ID">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="from" class="col-sm-2 text-right control-label col-form-label">Loan Date From</label>
                                    <div class="col-sm-4 input-group">
                                        <input type="text" class="form-control" id="datepicker-autoclose" placeholder="mm/dd/yyyy">
                                        <div class="input-group-append">
                                            <span class="input-group-text"><i class="fa fa-calendar"></i></span>
                                        </div>
                                    </div>
                                    <label for="to" class="col-sm-1 text-right control-label col-form-label">To</label>
                                    <div class="col-sm-4 input-group">
                                        <input type="text" class="form-control" id="datepicker-autoclose1" placeholder="mm/dd/yyyy">
                                        <div class="input-group-append">
                                            <span class="input-group-text"><i class="fa fa-calendar"></i></span>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="reason" class="col-sm-2 text-right control-label col-form-label">Reason for Loan</label>
                                    <div class="col-sm-6">
                                        <textarea rows="3" class="form-control" id="reason" name="reason"></textarea>
                                    </div> 
                                </div>
                                <div class="form-group row">
                                    <label for="attach" class="col-sm-2 text-right control-label col-form-label">Attachment</label>
                                    <div class="col-sm-6">
                                        <div class="custom-file">
                                            <label class="custom-file-label" for="validatedCustomFile" placeholder="Choose File.."></label>
                                            <input type="file" class="custom-file-input" id="validatedCustomFile" name="validatedCustomFile" required>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="comment" class="col-sm-2 text-right control-label col-form-label">Equiment Configuration</label>
                                    <div class="col-sm-9">
                                        <textarea rows="5" class="form-control" id="comment" name="comment"></textarea>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="shipping" class="col-sm-2 text-right control-label col-form-label">Shipping Address</label>
                                    <div class="col-sm-9">
                                        <textarea rows="3" class="form-control" id="shipping" name="shipping"></textarea>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="return" class="col-sm-2 text-right control-label col-form-label">Return Address</label>
                                    <div class="col-sm-9">
                                        <textarea rows="3" class="form-control" id="return" name="return" disabled>Motorola Solutions Penang</textarea>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="inven" class="col-sm-2 text-right control-label col-form-label">Inventory List</label>
                                    <div class="col-sm-8"></div>
                                    <div class="col-sm-2">
                                        <button type="button" class="btn btn-primary" onclick="addTable()">Add</button>
                                        <button type="button" class="btn btn-danger" onclick="deleteTable()">Delete</button>
                                    </div>
                                    <br>

                                    <!-- <div class="col-sm-2"></div> -->
                                    <div class="col-sm-12">
                                        <table class="table" id="tableList">
                                            <thead>
                                                <tr>
                                                <th scope="col">Category</th>
                                                <th scope="col">Item</th>
                                                <th scope="col">Sales Model No.</th>
                                                <th scope="col">Asset Tag</th>
                                                <th scope="col">Serial No.</th>
                                                <th scope="col">Quantity</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="border-top">
                                <div class="card-body">
                                    <button type="submit" class="btn btn-primary">Submit</button>
                                    <button type="button" id="test" class="btn btn-primary">Test</button>
                                    <button type="reset" class="btn btn-info">Reset</button>
                                </div>
                            </div>
                        </form>

The action is directly pointed to a uploadFile.java and I have no idea how to upload the file into a database.

I have a couple question about uploading to database :
1. Upload the file directly to database
2. Upload the file to glassfish4 local server, and save the path into database
After uploading to database, I would like to retrieve the file, and let the other user to download it.

I would like to know which 1 is easier to achieve? And which 1 is the more correct approach.

Reggie
  • 49
  • 10

1 Answers1

0

So files in a database would need to be saved in a blob or some other db specific large field implementation, and on the server, you can just created a file system, then save the path in the database. This is my preferred method, as the the db stays responsive when using that table.

if you're running the server locally, you just need to grant access to the computer's file system, so that the file can be saved on there.

examples of saving files on a server with java are;

save uploaded file at server

How do I create a file and write to it in Java?

https://stackabuse.com/reading-and-writing-files-in-java/

Can I suggest that you have a good read on java and file systems before you go through with your implementation.

juju
  • 553
  • 1
  • 4
  • 21
  • Hi , thanks for the recommendations, I will read through all the document. Last time while I was doing Django, I also used to save the file path and store the file in server, the first link that you provided, does the file type matters ?? Or any file type will do ?? – Reggie Jun 21 '19 at 00:54
  • The way I want to achieve is not reading / writing file, just need to directly upload the file that user chooses, directly to the server. And in the future, the file can be downloaded back from the user, that's all. – Reggie Jun 21 '19 at 00:57
  • To 'upload', you first need to read the file, then write it, that's how java works. You create a stream, then write to disk from that stream. And then downloading, the same, but the other way around. If the answer was helpful, please mark as solution, you should have all you need from these examples. – juju Jun 21 '19 at 08:44