我在我的注册表单中上传用户图像。我想将它保存在图像文件夹中。如果给定绝对路径,我的应用程序正在工作,但如何给路径相对方式。这是我的代码。//检查多部分内容* // if (!ServletFileUpload.isMultipartContent(request)) //if1
{
request.setAttribute("error", "No file is selected for upload");
rd=request.getRequestDispatcher("error.jsp"); //dispatching to error page if no file is sent via request
rd.forward(request,response);
} //end of if1
//****Setting space and path where file will be uploaded on server*****//
DiskFileItemFactory factory = new DiskFileItemFactory(); //object of class defined in commons package
factory.setSizeThreshold(THRESHOLD_SIZE);
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE);
upload.setSizeMax(REQUEST_SIZE);
String uploadPath = "c://"+ File.separator + UPLOAD_DIRECTORY;
// creates the directory if it does not exist
File uploadDir = new File(uploadPath);
userbean.setUserimage(uploadPath);
if (!uploadDir.exists())//if2
{
uploadDir.mkdir();
} //end of if2
//*******check the type of form and process accordingly*****//
try
{ //try1
List formItems = upload.parseRequest(request);// parses the request's content to extract file data
Iterator iter = formItems.iterator();
//******* iterates over form's fields**********//
while (iter.hasNext()) //while1
{
FileItem item = (FileItem) iter.next();
// ********processes only fields that are not form fields*******//
if (!item.isFormField()) //if3
{
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
// saves the file on disk
item.write(storeFile);
fileuploaded=true;
}//end of if3