[英]How to upload file using java ?
您好,我試圖使用Java文件上傳文件。但是我沒有得到它。我得到的文件大小為0,我在這里提供我的Java代碼。 告訴我為什么我不能上傳到特定的文件夾。 我想將我的文件存儲在特定的文件夾中。 我正在嘗試獲取文件大小,文件名,但我在錯誤的地方輸入了空值,請告訴我。
public void updateTesti(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, PortletException
{
//image upload logic
String folder_for_upload =(getPortletContext().getRealPath("/"));
//String folder=actionRequest.getParameter("uploadfolder");
realPath=getPortletContext().getRealPath("/");
logger.info("RealPath is" + realPath);
logger.info("Folder is :" + folder_for_upload);
try
{
logger.info("Admin is try to upload");
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
if (uploadRequest.getSize("fileName") == 0) {
SessionErrors.add(actionRequest, "error");
}
String sourceFileName = uploadRequest.getFileName("fileName");
File uploadedFile = uploadRequest.getFile("fileName");
System.out.println("Size of uploaded file: " + uploadRequest.getSize("fileName"));
logger.info("Uploded file name is: " + uploadRequest.getFileName("fileName"));
String destiFolder=("/home/ubuntu/liferay/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/webapps/imageUpload-portlet/image");
String newsourcefilename = (uploadRequest.getFileName("fileName"));
File newFile = new File(destiFolder +"/"+ newsourcefilename);
logger.info("New file name: " + newFile.getName());
logger.info("New file path: " + newFile.getPath());
InputStream in = new BufferedInputStream(uploadRequest.getFileAsStream("fileName"));
FileInputStream fis = new FileInputStream(uploadedFile);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] bytes_ = FileUtil.getBytes(in);
int i = fis.read(bytes_);
while (i != -1) {
fos.write(bytes_, 0, i);
i = fis.read(bytes_);
}
fis.close();
fos.close();
Float size = (float) newFile.length();
System.out.println("file size bytes:" + size);
System.out.println("file size Mb:" + size / 1048576);
logger.info("File created: " + newFile.getName());
SessionMessages.add(actionRequest, "success");
}
catch (FileNotFoundException e)
{
System.out.println("File Not Found.");
e.printStackTrace();
SessionMessages.add(actionRequest, "error");
}
catch (NullPointerException e)
{
System.out.println("File Not Found");
e.printStackTrace();
SessionMessages.add(actionRequest, "error");
}
catch (IOException e1)
{
System.out.println("Error Reading The File.");
SessionMessages.add(actionRequest, "error");
e1.printStackTrace();
}
}
您需要執行此操作以上傳小於1kb的小文件
File f2 = uploadRequest.getFile("fileupload", true);
它們僅存儲在內存中。 如果我得到一個空指針,或者在我的原始文件(f1.length)== 0的情況下,我在catch語句中有它
我已經執行了您的代碼。它按預期運行。您的jsp頁面可能有問題。我不確定,但是可能是您的name屬性與您在processAction
中使用的屬性不同(假設您在使用portlet)。參數區分大小寫,因此請再次檢查。
您將在下面的鏈接中找到更多信息。文件上傳中有很好的解釋。
http://www.codeyouneed.com/liferay-portlet-file-upload-tutorial/
我檢查了一個文件上傳代碼,當我在本地系統中實現該代碼時,portlet將我上傳的文件保存在tomcat/webbapp/abc_portlet_project
位置中,我不了解的是在哪里找到了portlet
字符串文件夾= getInitParameter(“ uploadFolder”);
String realPath = getPortletContext().getRealPath("/"); System.out.println("RealPath" + realPath +"\\\\" + folder); try {
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest); System.out.println(“ Size:” + uploadRequest.getSize(“ fileName”)));
如果(uploadRequest.getSize(“ fileName”)== 0){SessionErrors.add(actionRequest,“ error”);}
字符串sourceFileName = uploadRequest.getFileName(“ fileName”); 文件file = uploadRequest.getFile(“ fileName”);
System.out.println(“ Nome file:” + uploadRequest.getFileName(“ fileName”)); 文件newFolder = null; newFolder =新文件(realPath +“ \\” +文件夾); if(!newFolder.exists()){newFolder.mkdir(); }文件newfile = null; newfile =新文件(newFolder.getAbsoluteFile()+“ \\” + sourceFileName); System.out.println(“新文件名:” + newfile.getName()); System.out.println(“新文件路徑:” + newfile.getPath());
InputStream in = new BufferedInputStream(uploadRequest.getFileAsStream(“ fileName”)); FileInputStream fis =新的FileInputStream(file); FileOutputStream fos = new FileOutputStream(newfile);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.