你好我试图使用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();
}
}
答案 0 :(得分:1)
你需要这样做才能上传小文件< 1kb的
File f2 = uploadRequest.getFile("fileupload", true);
它们仅存储在内存中。我在我的catch语句中有它,因为我得到一个空指针 - 或者包含我的原始文件(f1.length)== 0
答案 1 :(得分:0)
我已经执行了你的代码。它正在按照期望工作。你的jsp页面可能有问题。我不确定但你的名字属性可能与你在{{1 (假设您正在使用portlet).Parameter区分大小写,因此请再次检查。
您可以在下面的链接中找到更多信息。在文件上传方面有很好的解释。
http://www.codeyouneed.com/liferay-portlet-file-upload-tutorial/
答案 2 :(得分:-1)
我浏览了一个文件上传代码,当我在我的本地系统中实现我得到的是,portlet正在保存我在tomcat/webbapp/abc_portlet_project
位置上传的文件,我不明白的是从哪里找到portlet < / p>
String folder = getInitParameter(&#34; uploadFolder&#34;);
String realPath = getPortletContext().getRealPath("/"); System.out.println("RealPath" + realPath +"\\" + folder); try {
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(的ActionRequest); System.out.println(&#34;大小:&#34; + uploadRequest.getSize(&#34; fileName&#34;));
if(uploadRequest.getSize(&#34; fileName&#34;)== 0) {SessionErrors.add(actionRequest,&#34; error&#34;);}
String sourceFileName = uploadRequest.getFileName(&#34; fileName&#34;);文件 file = uploadRequest.getFile(&#34; fileName&#34;);
System.out.println(&#34; Nome文件:&#34; + uploadRequest.getFileName(&#34;文件名&#34));文件newFolder = null; newFolder =新文件(realPath +&#34; \&#34; +文件夹); if(!newFolder.exists()){newFolder.mkdir(); } File newfile = null; newfile = new File(newFolder.getAbsoluteFile()+&#34; \&#34; + sourceFileName); System.out.println(&#34;新文件名:&#34; + newfile.getName()); System.out.println(&#34;新文件路径:&#34; + newfile.getPath());
InputStream in = new 的BufferedInputStream(uploadRequest.getFileAsStream(&#34;文件名&#34)); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(newfile);