我正在開發和使用eclipse IDE的應用程序。我的應用程序具有文件上傳功能。上傳的文件沒有保存到webcontent目錄
我能夠實現如何上傳文件並保存它。但問題是上傳的文件沒有存儲到我的動態Web項目目錄中。
文件上傳商店商品到我的服務器目錄具有路徑.metadata
文件夾
文件:/// E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/ wtpwebapps/
我想我的上傳文件夾,存儲到具有含圖片文件夾,像WebContent/upload/images
文件夾上傳我的WebContent文件夾。
毫無疑問,我可以查看圖像文件,但是我想要的路徑就像上面那樣。 下面的代碼我使用的存儲上傳的文件
@RequestMapping(value = "/company/UploadFile.action", method = RequestMethod.POST)
public @ResponseBody String uploadFile(FileUploadBean uploadItem, BindingResult result,HttpServletRequest request, HttpServletResponse response) {
System.out.println("FILE UPLOAD ITEM SI SSLSL ::"+uploadItem);
ExtJSFormResult extjsFormResult = new ExtJSFormResult();
if (result.hasErrors()){
for(ObjectError error : result.getAllErrors()){
System.err.println("Error: " + error.getCode() + " - " + error.getDefaultMessage());
}
//set extjs return - error
extjsFormResult.setSuccess(false);
return extjsFormResult.toString();
}
// Some type of file processing...
System.err.println("-------------------------------------------");
System.err.println("Test upload: " + uploadItem.getFile().getOriginalFilename());
System.err.println("-------------------------------------------");
try{
MultipartFile file = uploadItem.getFile();
String fileName = null;
InputStream inputStream = null;
OutputStream outputStream = null;
if (file.getSize() > 0) {
inputStream = file.getInputStream();
/*if (file.getSize() > 10000) {
System.out.println("File Size:::" + file.getSize());
extjsFormResult.setSuccess(false);
return extjsFormResult.toString();
}*/
System.out.println("also path ::"+request.getRealPath("") + "/upload/images/");
System.out.println("PATHI SIS SIS"+this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
System.out.println("size::" + file.getSize());
InetAddress addr = InetAddress.getLocalHost();
byte[] ipAddr = addr.getAddress();
System.out.println("HOST NAME"+request.getRealPath("ResourceMgt"));
System.out.println("HOST ADDR"+addr.getHostAddress());
System.out.println("HOST "+request.getRequestURI());
System.out.println("HOST "+request.getRequestURL());
fileName = request.getRealPath("") + "/upload/images/"
+ file.getOriginalFilename();
outputStream = new FileOutputStream(fileName);
System.out.println("FILEN ANEM AND PATH IS ::"+fileName);
System.out.println("fileName:" + file.getOriginalFilename());
int readBytes = 0;
byte[] buffer = new byte[40000];
while ((readBytes = inputStream.read(buffer, 0, 40000)) != -1) {
outputStream.write(buffer, 0, readBytes);
}
companyservice.saveImages(file.getOriginalFilename(),fileName);
outputStream.close();
inputStream.close();
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
//set extjs return - sucsess
extjsFormResult.setSuccess(true);
return extjsFormResult.toString();
}
請建議我如何可以存儲上傳到具有圖片上傳文件夾的文件夾我的WebContent文件。我上面的代碼工作完美只是有一些指定路徑的問題。
能否請你告訴我,我怎樣才能在我的Java類 – 2012-03-22 12:21:06
那麼在WebContent路徑,它實際上還要考慮那種你正在開發的應用程序。但是...經歷這個頁面:http://forums.whirlpool.net.au/archive/1232043 – 2012-03-22 13:40:42