Marty Griffin
Marty Griffin

Reputation: 349

File uploading in struts 2 yields files with .tmp extensions

This is what I have so far. I know the file works because when I force the .jpg extension and upload a .jpg image it works fine. But the filename looks like this with the appended .jpg.

E:\Documents and Settings\mgrif002\Desktop\data\upload_59fef24e_1302d1a3fb9__7ff5_00000001.tmp.jpg

try 
{
    String filePath = "E:/Documents and Settings/mgrif002/Desktop/data/";
    //System.out.println("Server path:" + filePath);
    System.out.println(form.getFile().getAbsolutePath());
    File fileToCreate = new File(filePath,form.getFile().getAbsolutePath()+".jpg");

    FileUtils.copyFile(form.getFile(), fileToCreate);

    Dicom dicom_file = new Dicom(pid,fileToCreate.getAbsolutePath(), form.getName(),true,((short)1));
    ccr.saveDicom(dicom_file);
} 
catch (IOException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}  

Upvotes: 3

Views: 2272

Answers (2)

Will Berger
Will Berger

Reputation: 31

this was killing me as well. This is how I solved the issue. Thought this might save someone a few precious ours of their lives :)

if (request instanceof MultiPartRequestWrapper) {
                MultiPartRequestWrapper multiWrapper =
                    (MultiPartRequestWrapper) request;
                if (multiWrapper != null) {
                    HttpServletRequest hrequest = (HttpServletRequest)request;

                    Enumeration fileParameterNames = multiWrapper.getFileParameterNames();              
                    //String fileName = multiWrapper.getFileNames("upload")[0];
                    if(fileParameterNames.hasMoreElements()){
                        String inputValue = (String) fileParameterNames.nextElement();
                        String[] localFileNames = multiWrapper.getFileNames(inputValue);
                        for (String fn : localFileNames) {
                            System.out.println("local filename = " + fn);                   
                        }
                        File[] files = multiWrapper.getFiles(inputValue);
                        HttpSession ses = hrequest.getSession();
                        User user = (User) ses.getAttribute("user");
                        IR loggedInIR = user.getIr();

                        for (File cf : files) {
                            File destFile = new File(cf.getParentFile().getAbsolutePath()+"\\"+loggedInIR.getId()+"\\images\\"+localFileNames[0]);
                            FileUtils.copyFile(cf, destFile);
                            FileUtils.deleteQuietly(cf);

                        }               
                    }
                }
            }

Upvotes: 3

BalusC
BalusC

Reputation: 1108702

Just replace .tmp by .jpg instead of appending .jpg. There are several ways, such as substringing the part until the last period, or regex replacing the last .tmp part, etc.

String fileName = form.getFile().getName().replaceAll("\\.tmp$", ".jpg");
File fileToCreate = new File(filePath, fileName);
// ...

Upvotes: 1

Related Questions