0

I am new to Struts and working on File Upload using Struts.

Client: It is Java Program which hits my Strut app by using apache HttpClient API and provides me File. Client as per need sometime gives me .wav file and sometime .zip file and sometime both.

Server: Struts app which got the request from client app and upload the file.

Here, problem comes as I upload the file, it get uploaded using ".tmp" extension, which I want to get uploaded with the same extension what client has passed.

Or there is any other way by which we can check what is the extension of the file client has sent....?

I am stuck in this problem and not able to go ahead.

Please Find the code attached and tell me what modification I have to do:

Server Code:

    MultiPartRequestWrapper multiWrapper=null;
    File baseFile=null;

    System.out.println("inside do post");

    multiWrapper = ((MultiPartRequestWrapper)request);


    Enumeration e = multiWrapper.getFileParameterNames();

    while (e.hasMoreElements()) {
        // get the value of this input tag
        String inputValue = (String) e.nextElement();

        // Get a File object for the uploaded File
        File[] file = multiWrapper.getFiles(inputValue);

        // If it's null the upload failed
        if (file != null) {


            FileInputStream fis=new FileInputStream(file[0]);

            System.out.println(file[0].getAbsolutePath());
            System.out.println(fis);

            int ch;
            while((ch=fis.read())!=-1){
                System.out.print((char)ch);
            }
        }

    }

    System.out.println("III :"+multiWrapper.getParameter("method"));

Client code:

 HttpClient client = new HttpClient();
                  MultipartPostMethod mPost = new MultipartPostMethod(url);
                        File zipFile = new File("D:\\a.zip");
                        File wavFile = new File("D:\\b.wav");
                        mPost.addParameter("recipientFile", zipFile);
                        mPost.addParameter("promptFile", wavFile);
                        mPost.addParameter("method", "addCampaign");
  statusCode1 = client.executeMethod(mPost);

actually Client is written long back and cant be modified and I want to identify something at server side only to find the extension.

Please help, Thanks.

Jayesh
  • 6,047
  • 13
  • 49
  • 81
  • is it in struts2 or struts1? in struts2 while uploading file ,file upload interceptor send on property namely `contentType` by which you can always fine the file type.Its always better to provide some sort of code with the question – Umesh Awasthi Jul 27 '12 at 06:00
  • In struts2 you have to check the contentType with MIME types. If its contentType is `audio/x-wav` the file will have `.wav` extension and if the file is `application/zip` the file will have `.zip` extension. – Pigueiras Jul 27 '12 at 06:04

1 Answers1

1

Struts2 File Uploader interceptor when uploading file pass the content type information to the Action class and one can easily find the file type by comparing contentType with MIME type.

If you want to can create a map with key as content type and file type as its value like

map.Add("image/bmp",".bmp", )
map.Add("image/gif",".gif", )
map.Add("image/jpeg",".jpeg", )

and can easily fetch the type based on the extension provides.Hope this will help you.

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
  • @user1012372: why don't you add file extension type using java code like `mPost.addParameter("fileExtension", zipFile .extension());` or you can use same ContentType to get file extension as describe din my post – Umesh Awasthi Jul 27 '12 at 06:35
  • Umesh: Client is not under my control thatsy. Please help at Server side code... – Jayesh Jul 27 '12 at 07:39
  • Also I tried this Umesh, File[] file = multiWrapper.getFiles(inputValue); // If it's null the upload failed if (file != null) { if(file[0].getContentType().equals("application/zip")) { } } but getContentType() is not present inside File Class. – Jayesh Jul 27 '12 at 07:46
  • contentType is being provided by Struts2 file uploader interceptor as a property,you need to define a property in your action class as contentType and let Struts2 file Uploader provides contentType information for you – Umesh Awasthi Jul 27 '12 at 08:05