问题描述
|||
到目前为止,这就是我所拥有的。我知道该文件有效,因为当我强制.jpg扩展名并上传.jpg图像时,它可以正常工作。但是文件名看起来像这样,带有附加的.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();
}
解决方法
只需将
.tmp
替换为.jpg
,而不要附加.jpg
。有几种方法,例如,将子字符串串到最后一个句号,或用正则表达式替换最后的“ 1”部分,等等。
String fileName = form.getFile().getName().replaceAll(\"\\\\.tmp$\",\".jpg\");
File fileToCreate = new File(filePath,fileName);
// ...
,这也杀死了我。这就是我解决问题的方式。认为这可以为某人节省一些宝贵的生命:)
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);
}
}
}
}