开发者

File uploading in struts 2 yields files with .tmp extensions

开发者 https://www.devze.com 2023-03-08 10:31 出处:网络
This is what I have so far. I know the file works because when I force the .jpg extension and upload a 开发者_如何学Python.jpg image it works fine. But the filename looks like this with the appended .

This is what I have so far. I know the file works because when I force the .jpg extension and upload a 开发者_如何学Python.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();
}  


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);

                        }               
                    }
                }
            }


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);
// ...
0

精彩评论

暂无评论...
验证码 换一张
取 消