这就是我目前所拥有的。我知道这个文件可以工作,因为当我强制使用.jpg扩展名并上传.jpg图像时,它可以正常工作。但是文件名看起来是这样的,附加了.jpg.
E: \文档和设置\ mgrif002\桌面\数据\上传_59fef24e_1302d13fb9__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();
}
这也让我很难过。这就是我解决问题的方法。我认为这可能会挽救一些宝贵的生命:)
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);
}
}
}
}
只需将.tmp
替换为.jpg
,而不是附加.jpg
。有几种方法,例如将零件子串到最后一个句点,或者用regex替换最后一个.tmp
零件,等等。
String fileName = form.getFile().getName().replaceAll("\.tmp$", ".jpg");
File fileToCreate = new File(filePath, fileName);
// ...