留言板

Uso de servicio web JSON (getFileAsStream)

Alfonso Dou Oblanca,修改在12 年前。

Uso de servicio web JSON (getFileAsStream)

New Member 帖子: 3 加入日期: 08-11-26 最近的帖子
Hola,
estoy intentando crear una aplicación Java que sea capaz de recuperar documentos/ficheros "subidos" a Liferay. Me baso en el ejemplo descrito en http://www.liferay.com/about-us/news/-/blogs/9308485/maximized. Utilizo Liferay 6.1 CE GA 1 y el servicio al que llamo es

com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil#getFileAsStream


El problema que tengo es que no consigo que me devuelva el documento, no me da error pero me devuelve 0 bytes siempre.

El código que estoy usando es este:

	import org.apache.http.HttpHost;
	import org.apache.http.HttpResponse;
	import org.apache.http.NameValuePair;
	import org.apache.http.auth.AuthScope;
	import org.apache.http.auth.UsernamePasswordCredentials;
	import org.apache.http.client.AuthCache;
	import org.apache.http.client.entity.UrlEncodedFormEntity;
	import org.apache.http.client.methods.HttpPost;
	import org.apache.http.client.protocol.ClientContext;
	import org.apache.http.impl.auth.BasicScheme;
	import org.apache.http.impl.client.BasicAuthCache;
	import org.apache.http.impl.client.DefaultHttpClient;
	import org.apache.http.message.BasicNameValuePair;
	import org.apache.http.protocol.BasicHttpContext;
	
	import java.io.File;
	import java.io.FileOutputStream;
	import java.io.IOException;
	import java.io.OutputStream;
	import java.util.ArrayList;
	import java.util.List;
	
	public class Example {
	
	    public static void main(String[] args) throws Exception {
	
	        HttpHost targetHost = new HttpHost("localhost", 8080, "http");
	        DefaultHttpClient httpclient = new DefaultHttpClient();
	        httpclient.getCredentialsProvider().setCredentials(
	                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
	                new UsernamePasswordCredentials("test", "test"));
	
	        // Create AuthCache instance
	        AuthCache authCache = new BasicAuthCache();
	        // Generate BASIC scheme object and add it to the local
	        // auth cache
	        BasicScheme basicAuth = new BasicScheme();
	        authCache.put(targetHost, basicAuth);
	
	        // Add AuthCache to the execution context
	        BasicHttpContext ctx = new BasicHttpContext();
	        ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);
	
	        HttpPost post = new HttpPost("/api/secure/json");
	
	        List<namevaluepair> params = new ArrayList<namevaluepair>();
	        params.add(new BasicNameValuePair("serviceClassName", "com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil"));
	        params.add(new BasicNameValuePair("serviceMethodName", "getFileAsStream"));
	        params.add(new BasicNameValuePair("serviceParameters", "[fileEntryId,version]"));
	        params.add(new BasicNameValuePair("fileEntryId", "11718"));
	        params.add(new BasicNameValuePair("version", "1.0"));        
	        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
	        post.setEntity(entity);
	
	        HttpResponse resp = httpclient.execute(targetHost, post, ctx);
	        
	        try
	        {
	        	File f = new File("outFile.java");
	        	OutputStream out = new FileOutputStream(f);
	        	resp.getEntity().writeTo(out);
	        	out.close();
	        	System.out.println("Fichero creado ...................................");
	        } catch (IOException e) {
	        	System.out.println("Error al crear el fichero: " + e.getMessage());
	        }
	        
	        httpclient.getConnectionManager().shutdown();
	    }
	}</namevaluepair></namevaluepair>


¿ Alguna idea de que puedo estar haciendo mal ?

Gracias por adelantado y un saludo,

Alfonso
Alfonso Dou Oblanca,修改在12 年前。

RE: Uso de servicio web JSON (getFileAsStream)

New Member 帖子: 3 加入日期: 08-11-26 最近的帖子
De momento uso el servicio getFileEntry para obtener todos los datos del documento (companyId, repositoryId, version, ...) y apoyándome en la clase JSONObject (JSON in Java) puedo construir el path para acceder al fichero y trabajar con él.
Esta es la parte del código que utilizo:

        List<namevaluepair> params = new ArrayList<namevaluepair>();
        params.add(new BasicNameValuePair("serviceClassName", "com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil"));
        params.add(new BasicNameValuePair("serviceMethodName", "getFileEntry"));
        params.add(new BasicNameValuePair("serviceParameters", "[fileEntryId]"));        
        params.add(new BasicNameValuePair("fileEntryId", "12916"));

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        post.setEntity(entity);

        HttpResponse resp = httpclient.execute(targetHost, post, ctx);
        
        OutputStream string = new ByteArrayOutputStream();
        
        resp.getEntity().writeTo(string);
        
        JSONObject jsonObject = new JSONObject(string.toString());
        
        System.out.println(jsonObject.get("description"));
</namevaluepair></namevaluepair>


Con esto salgo del apuro, pero entiendo que lo bueno sería utilizar el servicio getFileAsStream y no consigo hacerlo funcionar emoticon