?
Solved

upload a file to server

Posted on 2011-02-22
12
Medium Priority
?
749 Views
Last Modified: 2012-05-11

i had a form and i am browsing a file from it and sending it to server....
I want to upload it to server... and need to parse it values...

now i can do it when i have my server and client on one machine..
so can some one tell me how to upload a file to a server and

use it in

File scpath;
saxParser.parse(scpath, handler);


I came to know that it can be done using commons file upload.jar
can some one tell me how to use...it and send that file to my saxparser...
0
Comment
Question by:shragi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34955436
If you create a servlet with a form which will have
an element to browse for the file and upload it, then
you write what you uploaded to some location on your server assign any filename to it
and then feed filename of what you wrote to the SAX Parser - then
it should not  matter where is your client -  it should work for any client.

As one can understand from your question ,you already
know  how to upload the file to the server.
Then oncee you do it within a servlet - should not be any issue.

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34955566

This is about uploading the file from the dform and reading it
in the servlet:

I do uploading the file in this way - in the form I place such element:
<input type="file" name="myFile" align="Right" size="40" id="peek" maxlength="0">

then on the next page-servlet where I read input from the form, i have such code:

public synchronized void doPost(HttpServletRequest req, HttpServletResponse res)
 throws IOException {

MultiPartRequest mpr = new MultiPartRequest(req);

  DataInputStream inputSDF = new DataInputStream(new BufferedInputStream(mpr.getInputStream("myFile")));


and  then you have stream from which you read this file.

Open in new window







0
 
LVL 92

Expert Comment

by:objects
ID: 34956016
0
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 

Author Comment

by:shragi
ID: 34956304
<html>
<head></head>
<body>
      <p>Commons File Upload Example</p>
      <form action="Commonsfileuploadservlet" enctype="multipart/form-data" method="POST">
            <input type="file" name="file1"><br>
            <input type="Submit" value="Upload File" onclick="uploadAction()"><br>
      </form>
</body>
<script language="JavaScript" type="text/javascript">
      function uploadAction() {
            document.forms[0].action = "uploadFile.do?method=uploadFile";
            document.forms[0].submit();
      }
</script>
</html>


and in action class of uploadFile I wrote below code...


DiskFileItemFactory  fileItemFactory = new DiskFileItemFactory ();
ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
try {
      List  items = (List) upload.parseRequest(request);
      DataInputStream inputSDF = new DataInputStream(new BufferedInputStream(------------);//.getInputStream("myFile")));
      saxParser.parse(inputSDF, handler);
}
catch (FileUploadException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
}

what should i change in the above code... and what can be place in the below line..
DataInputStream inputSDF = new DataInputStream(new BufferedInputStream(------------);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34956396


I think your list may contain several items of your form,in this case it has
just one item - so you want to get the first item form your list  

after you parse it (List  items = (List) upload.parseRequest(request);), so
you say:

  FileItem item = (FileItem) items.get(0);

  and then
 DataInputStream inputSDF = new DataInputStream(new BufferedInputStream(item.getInputStream());

0
 
LVL 92

Expert Comment

by:objects
ID: 34956439
Iterator iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();

    if (item.isFormField()) {
    } else {
        saxParser.parse(new InputSource(item.getInputStream()), handler);
    }
}
0
 

Author Comment

by:shragi
ID: 34956797
these samples are gud for reading the file....

but what am i doing is...

when i have client and server on same machine...
i give the path to server so it uploads the file to server....

now if i have the stream i can just read the stream....

the thing is when i set the path to a document variable
 document.setFile("C://test.txt");

it creates a file in server....

so instead of stream can i get file as output...
otherwise is it possible to create file from stream... with out any temp... files...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34956847
Of course, this is the input stream - why not to read from it and write to the output stream
and the output file?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 34956894
This i how I do it:

   DataInputStream inputSDF = new DataInputStream(new BufferedInputStream(item.getInputStream());

        FileOutputStream outputSDF = new FileOutputStream(filesForPMS + request + "_" + fileName.replace(' ','_'));

    
        byte [] buffer;
        int bytes_read;

         buffer = new byte[500000];
                 int countRead = 0;
      

                        while(true) {
                            bytes_read = inputSDF.read(buffer,0,buffer.length);
                            if(countRead == 0 && bytes_read == -1){
                                                        break;
                            } else if (bytes_read == -1) break;
                                                        outputSDF.write(buffer, 0, bytes_read);
                            countRead++;
                    }




              inputSDF.close();
        outputSDF.close();

Open in new window




I guess, the only thing you would not know is the file name and extension -
I assign some name myself.
If you know the type of file, it helps.

But all these things are happening on the server side - client has nothing to do
with all that - they just upload file from their computer - you receive the stream and
write new file on the server



0
 

Author Comment

by:shragi
ID: 34956976
ok let me place my question clearly...

--connection established---
document.setObjectName( "file234" );
document.setContentType( "text" );
document.setFile("C://test.txt");
document.link("/Temp");               //destination folder in server
document.save();


now when i execute teh above code.... a new file with name "file234" is created...

I understood ur code in the below way....

you read inputstream byte by byte and wrote in output stream...
whats confusing for me is to what file name you wrote the output stream...
and can we directly use that filename for the below function...

document.setFile("C://test.txt");

0
 
LVL 92

Expert Comment

by:objects
ID: 34957074
to create a file all you need is


    File uploadedFile = new File("C://test.txt");
    item.write(uploadedFile);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34957102
And you can write to any filename you choose.
Like "C://text.txt" if you want.

I pasted a real example and I want to get files uploaded
from different clients under different names, therefore I have a special
way of constructing file name using unique request number so that
files from different users do not overwrite each other.
If you do it once, then "C://text.txt" would be fine.
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question