Fórum

File Upload for Custom Portlet

thumbnail
Karma Coma, modificado 12 Anos atrás.

File Upload for Custom Portlet

New Member Postagens: 12 Data de Entrada: 18/02/11 Postagens Recentes
Hi all,

I tried many of the ways to upload a file in a custom portlet but every one failed..
This makes me mad :/

Could anyone show me some working code example?
thumbnail
Mani kandan, modificado 12 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Hi karma,

Here the source code for Upload file in custom portlet.
I think you will finish it off.
thumbnail
Karma Coma, modificado 12 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 12 Data de Entrada: 18/02/11 Postagens Recentes
Thank you very much Mani! emoticon
thumbnail
Mani kandan, modificado 12 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
You are welcomeemoticon
Jhansi Rani, modificado 12 Anos atrás.

RE: File Upload for Custom Portlet

Junior Member Postagens: 32 Data de Entrada: 27/07/11 Postagens Recentes
Hi Mani,

I have done as you said in above examples, but I am getting NullPointer exception due to
I am getting length of bytes is null;
bytes = FileUtil.getBytes(file);
int length=bytes.length;

Thanks & Regards
Jhansi
thumbnail
Mani kandan, modificado 12 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Could you post you code here?
Jhansi Rani, modificado 12 Anos atrás.

RE: File Upload for Custom Portlet

Junior Member Postagens: 32 Data de Entrada: 27/07/11 Postagens Recentes
Hi Mani,

below is the code at
view.jsp
-------------

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

This is the <b>ImageUpload</b> portlet in View mode.
<form name="<portlet:namespace/>fm" method="post" enctype="multipart/form-data" action="<portlet:actionURL />" >
<table>
<tr>
<td>File : </td>

<td><input type="file" name="<portlet:namespace/>egfile" id="<portlet:namespace/>egfile" />
</td>
<td> <input type="hidden" name="hi" id="hi" value="Hi, HW r u?"/></td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Upload"/></td>
</tr>
</table>
</form>

ImageUpload.java
--------------------------
public void processAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {

String realPath = getPortletContext().getRealPath("/");
byte[] bytes = null;
System.out.println("real path^^^^^^^^^^^^"+realPath);
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
String sourceFileName =uploadRequest.getFileName("egfile");
String f=ParamUtil.getString(uploadRequest,"egfile");
File file=uploadRequest.getFile(sourceFileName);
String str=ParamUtil.getString(uploadRequest,"hi");
System.out.println("fileNme"+sourceFileName+"+++++++++++++++++str"+str);
try {
bytes = FileUtil.getBytes(file);
} catch (IOException e2) {
e2.printStackTrace();
}
System.out.println("length of file"+bytes.length);

}
thumbnail
Mani kandan, modificado 12 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Remove
File file=uploadRequest.getFile(sourceFileName);

Replace with this code,
File file = uploadRequest.getFile("egfile");
Jhansi Rani, modificado 12 Anos atrás.

RE: File Upload for Custom Portlet

Junior Member Postagens: 32 Data de Entrada: 27/07/11 Postagens Recentes
Hi Mani,

Thank you, I got it.

Thanks & Regards
Jhansi Rani P
thumbnail
Mani kandan, modificado 12 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
You welcome emoticon
Nguyen Le, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 11 Data de Entrada: 24/04/12 Postagens Recentes
I had received image and text at server. If i save the image, I can't insert this text to database. If I insert text to database, I can't save the image. Please help me to do both.
thumbnail
Mani kandan, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Could you share your code here?
Nguyen Le, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 11 Data de Entrada: 24/04/12 Postagens Recentes
Thanks 4 reply, I have uploaded a image and insert data from textfield to database.
Nguyen Le, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 11 Data de Entrada: 24/04/12 Postagens Recentes
Can you help me passing parameter between 2 portlet???
thumbnail
Mani kandan, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Unless seeing your code I cant do anything. Could you share your code here?
Nguyen Le, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 11 Data de Entrada: 24/04/12 Postagens Recentes
OK, I will send my code into your email. Please tell me your email. emoticon
thumbnail
Mani kandan, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
sachinmani07@gmail.com
Nguyen Le, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 11 Data de Entrada: 24/04/12 Postagens Recentes
Mani kandan:
sachinmani07@gmail.com



I was sent, check your email please.
thumbnail
Mani kandan, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
I uploaded working file upload war file..

I added getLiferayDatabaseConnection() function in your code(which means database connectivity using context.xml)
thumbnail
Tejas Patel, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Junior Member Postagens: 71 Data de Entrada: 24/01/12 Postagens Recentes
HI nguyen le,
I have same problem like your .
can you please give solution here how could you solve your problem.
Thanks in advance.
If any one else have solution then please share it here .
the problem is i want to add image as well as some text in database from user.
it does not work with enctype="multipart/form-data".
if i do not add this statment the text added and image not added,
if i add this statment the image added and the text does not get in process action class.
but my requirement is to get both thing from user and store in database.

Thanks and regards,
Tejas patel.
thumbnail
Tejas Patel, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Junior Member Postagens: 71 Data de Entrada: 24/01/12 Postagens Recentes
I got the answer by the following method.
File file=uploadRequest.getFile("fileName to get from jsp");
String name=uploadRequest.getParameter("name to get form jsp");

Thanks,
Tejas patel.
Nguyen Le, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 11 Data de Entrada: 24/04/12 Postagens Recentes
Tejas Patel:
I got the answer by the following method.
File file=uploadRequest.getFile("fileName to get from jsp");
String name=uploadRequest.getParameter("name to get form jsp");

Thanks,
Tejas patel.



Hi Tejas Patel, I have been uploaded file(image) and text(TextField) by using ParamUtil class. Do you want see it, email to me.
Nguyen Le, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 11 Data de Entrada: 24/04/12 Postagens Recentes
Tejas Patel:
HI nguyen le,
I have same problem like your .
can you please give solution here how could you solve your problem.
Thanks in advance.
If any one else have solution then please share it here .
the problem is i want to add image as well as some text in database from user.
it does not work with enctype="multipart/form-data".
if i do not add this statment the text added and image not added,
if i add this statment the image added and the text does not get in process action class.
but my requirement is to get both thing from user and store in database.

Thanks and regards,
Tejas patel.


Sorry, I late.

I will show my source code, you can references. If you have any problems, Please email to me(nguyenlx90@hotmail.com)

view.jsp
<input type="file" name="hotelImage" />
<input name="txtHotel" type="text" value="" size=60>

processAction:
public void processAction(ActionRequest req, ActionResponse res) throws PortletException {
try {
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(req);
File imageFile = uploadRequest.getFile("hotelImage");
String imageFileName = imageFile.getName();

String txtHotel = ParamUtil.getString(uploadRequest,"txtHotel");

...
// Save image to file system/insert to DB, insert text into DB is successed
} catch (Exception e1) {
e1.printStackTrace();
}
}
karthik reddy, modificado 10 Anos atrás.

RE: File Upload for Custom Portlet

Junior Member Postagens: 60 Data de Entrada: 08/04/13 Postagens Recentes
Hi,
I have 2 components in view.jsp. I want to send data in 2 components to JAVA file, save file on web server and insert data received from textfileld into database. Already i completed to store in server.Now my requirement is to store that image in database also with some primary key and also before storing into database when i choose that image it should display in the same page.Please give reply how to achieve this.


Thanks & Regards
M.Karthik
natanael berlian, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 14 Data de Entrada: 25/03/13 Postagens Recentes
hello manni, I've used your code to upload. portlet is success to run but I don't know, my file is success or not to upload. because there is no file in path folder i choose
can you help me?
this is my code
package com.git.upload;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import java.io.File;


import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletContext;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.util.PortalUtil;


public class uploadFile extends GenericPortlet{

	protected String viewJSP;
	protected String process;
	protected Vector<!--?--> v=null;
	protected String realPath = null;
	
	public void init() throws PortletException{
		viewJSP = getInitParameter("view-jsp");
	}
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
	{
		realPath = getPortletContext().getRealPath("/");
		System.out.println(realPath);
		
		byte[] bytes = null;
		PortletContext portletContext = request.getPortletSession().getPortletContext();
		try {
			UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
			String sourcefileName = uploadRequest.getFileName("fileName");
			File file = uploadRequest.getFile("fileName");
			try {
				bytes = FileUtil.getBytes(file);
				
			} catch (IOException e2) {
				e2.printStackTrace();// TODO: handle exception
			}
			File newFile= null;
			if((bytes != null) &amp;&amp; (bytes.length &gt; 0))
			{
				try {
					System.out.println(sourcefileName);
					
					newFile = new File(realPath+"html/uploads/"+sourcefileName);
					FileInputStream fileInputStream = new FileInputStream(file);
					FileOutputStream fileOutputStream = new FileOutputStream(newFile);
					fileInputStream.read(bytes);
					
					String value = new String(bytes);
					System.out.println(value);
					
					fileOutputStream.write(bytes,0,bytes.length);
					fileOutputStream.close();
					fileInputStream.close();
				} catch (FileNotFoundException e) {
					System.out.println("file not found");
					e.printStackTrace();// TODO: handle exception
				}
			}
		} catch (IOException e1) {
			System.out.println("Error reading the file");
			e1.printStackTrace();// TODO: handle exception
		}
	}
	
	public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException
	{
		doView(request, response);
	}
	public void doView(RenderRequest request, RenderResponse response) throws IOException
	{
		//private static Log _log = LogFactoryUtil.getLog(upload)
		String path=viewJSP;
		PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path);
		if(portletRequestDispatcher ==null){
			
		}
	}
}


thanks
thumbnail
Mani kandan, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Remove this code and check,

String value = new String(bytes);
System.out.println(value);
natanael berlian, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 14 Data de Entrada: 25/03/13 Postagens Recentes
Mani kandan:
Remove this code and check,

String value = new String(bytes);
System.out.println(value);



that is still not happening. this is my full source code and WAR.
thumbnail
Mani kandan, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Are you getting "sourcefileName" in the portlet class?

Just print sourcefileName in the portlet class
natanael berlian, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 14 Data de Entrada: 25/03/13 Postagens Recentes
Mani kandan:
Are you getting "sourcefileName" in the portlet class?

Just print sourcefileName in the portlet class



sourcefileName just string from class UploadPortletRequest

I'm sorry if I'm wrong, I'm new in Liferay..
thumbnail
Mani kandan, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Just give,
System.out.println(sourcefileName);
after
String sourcefileName = uploadRequest.getFileName("fileName");
and see wt is coming in the print
thumbnail
Mani kandan, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Try with this file
natanael berlian, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

New Member Postagens: 14 Data de Entrada: 25/03/13 Postagens Recentes
Mani kandan:
Try with this file



that's still not happening. I think this problem because my uploadportlet.java is not running. Is that true? because when I sysout the beginning of uploadportlet.java, there is no printed in console.
thumbnail
Mani kandan, modificado 11 Anos atrás.

RE: File Upload for Custom Portlet

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
Try this,

http://lauraliparulo.altervista.org/liferay-upload-file-portlet-working-example-with-upload-progress-bar/
lun wang, modificado 10 Anos atrás.

RE: File Upload for Custom Portlet

New Member Mensagem: 1 Data de Entrada: 31/07/13 Postagens Recentes
Hi,Mani now I use liferay MVCPortlet to upload file,
UploadPortletRequest upRequest = PortalUtil.getUploadPortletRequest(request);
File file = request.getFiles("uploadFile");
if (file != null) {
byte[] bytes = FileUtil.getBytes(file);
if ((bytes != null) && (bytes.length > 0)) {

String sourceFileName = file.getName();
String mimeType = MimeTypesUtil.getContentType(file);
ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), request);
FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(userId, repositoryId,FileConstants.MEETING_RECORD_FOLDER,
sourceFileName, mimeType, null, null, null,file, serviceContext);
}
when i ipload a txt,it fill failed ,i debug it found that no file in D:\liferay-portal-6.1.10-ee-ga1\tomcat-7.0.25\temp\ do not find uploadFile_0019436.txt
but when i load a doc or excl ,it will be ok!
i found when UploadPortletRequest upRequest = PortalUtil.getUploadPortletRequest(request); The upload file will copy to D:\liferay-portal-6.1.10-ee-ga1\tomcat-7.0.25\temp\ ,but when i upload a txt,it will not copy. Do you have any suggestion or example code.
i'm a chinese ,so my English is poor,i'm looking forword your advices, thank you!!
shrikanth shrikanth, modificado 10 Anos atrás.

RE: File Upload for Custom Portlet

Regular Member Postagens: 118 Data de Entrada: 11/12/13 Postagens Recentes
hello sir......

i want to upload image in MYSQL database.so plz help me if u have code plz send me iam using MVCportlet
thumbnail
ritresh girdhar, modificado 10 Anos atrás.

RE: File Upload for Custom Portlet

Junior Member Postagens: 67 Data de Entrada: 15/07/11 Postagens Recentes
Dear try This:

public void uploadCase(ActionRequest actionRequest,
ActionResponse actionRresponse) throws PortletException,
IOException {
String realPath = "C:\\Liferay 6.1.3\\liferay-portal-tomcat-6.1.2-ce-ga3-20130816114619181\\liferay-portal-6.1.2-ce-ga3\\data\\CandidatesExcel\\";
logger.info("RealPath" + realPath + " UploadFolder :");
try {
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
System.out.println("Size: "+uploadRequest.getSize("candidates"));
if (uploadRequest.getSize("candidates")==0) {
SessionErrors.add(actionRequest, "error");
}
String candidates []= uploadRequest.getFileNames("candidates");
File files[] = uploadRequest.getFiles("candidates");
logger.info("Nome file:" + uploadRequest.getFileName("candidates"));
ArrayList<File> newFiles=new ArrayList<>();
for(int j=0;j<files.length;j++)
{

File newFile = new File(realPath + candidates[j]);
System.out.println("newFile.getPath() "+newFile.getPath());
logger.info("New file name: " + newFile.getName());
logger.info("New file path: " + newFile.getPath());
InputStream in = new BufferedInputStream(uploadRequest.getFilesAsStream("candidates")[j]);
FileInputStream fis = new FileInputStream(files[j]);
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();
newFiles.add(newFile);
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();
}
}


JSP:


<portlet:actionURL var="uploadFileURL">
<portlet:param name="myActions" value="uploadMultipleFile"></portlet:param>
</portlet:actionURL>

<script type="text/javascript">
jQuery(function(){ // wait for document to load
jQuery('#uploadFile').MultiFile({
STRING: {
remove: '<img src="http://www.fyneworks.com/@/bin.gif" height="16" width="16" alt="x"/>'
}
});
});


function uploadNewFile()
{
jQuery('#fileTags').find('input[type="file"]').each(
function() {
var id = jQuery(this).attr('id');
jQuery(this).attr('name',id);
});
document.multipleUpload.action='<portlet:actionURL><portlet:param name="myActions" value="uploadMultipleFile"/></portlet:actionURL>';
document.multipleUpload.submit();
}
</script>




<form enctype="multipart/form-data" method="post" name="multipleUpload">
<table id="fileTags">
<tbody>
<tr><td>Upload File</td>
<td><input class="multi" id="uploadFile" name="uploadFile" type="file" />
</td>
<td></td>
</tr>
</tbody></table>
<input onclick="uploadNewFile()" type="button" value="Upload" />
</form>
thumbnail
meera prince, modificado 10 Anos atrás.

RE: File Upload for Custom Portlet

Liferay Legend Postagens: 1111 Data de Entrada: 08/02/11 Postagens Recentes
HI

The following is example portlet to upload image using BLOB data type in service builder
I have done this to 6.2 portal and same you can do for other version it will work as for my guess..

http://www.liferaysavvy.com/2013/12/using-blob-data-type-in-liferay-service.html


Regards,

Meera Prince

http://www.liferaysavvy.com