这是我到目前为止所拥有的。我知道该文件有效,因为当我强制.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();
}
答案 0 :(得分:3)
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);
}
}
}
}
答案 1 :(得分:0)
只需将.tmp
替换为.jpg
,而不是附加.jpg
。有几种方法,例如substringing期间的last部分,或regex替换最后的.tmp
部分等。
String fileName = form.getFile().getName().replaceAll("\\.tmp$", ".jpg");
File fileToCreate = new File(filePath, fileName);
// ...