Como informar o local para salvar arquivo retornado por um método Restful WebService

4 respostas
me1

Olá pessoal,

Eu tenho um metodo Restful Web Service que retorna um arquivo json, esse metodo recebe tres valores como parametro, sendo que o ultimo é o local onde eu desejo salvar o arquivo retornado. Meu problema é que eu não sei como especificar na url o local que eu quero salvar o arquivo no momento em que eu for invocar esse metodo.

Tentei assim:

http://localhost:8080/RodesRestfulWS/sigcdcls/transp/1/1/C:/my_first_transp
http://localhost:8080/RodesRestfulWS/sigcdcls/transp/1/1/C%3A%2Fmy_first_transp

Nesse exemplo eu tentei salvar o arquivo retornado no diretório “C:/my_first_transp”.

Grato.

4 Respostas

Ataxexe

Deixa eu ver se entendi: você quer que o serviço salve o arquivo no cliente?

me1

Exatamente, meu método gera um File e retorna através de um Response, então eu pego esse valor retornado para salvar em algum local no pc do cliente. Será que estou fazendo besteira?

package br.com.fourcontrol.resources;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import com.google.gson.stream.JsonWriter;

import br.com.fourcontrol.dao.SigCdClsDao;
import br.com.fourcontrol.model.SigCdCls;
import br.com.fourcontrol.model.SigCdCls.AtributosSIGCDCLS;
import br.com.fourcontrol.util.Constantes;

@Path("/sigcdcls")
public class SigCdClsResource extends ResourceRodes {

	@GET
	@Path("/transp" + Constantes.EMP_VEND_PATHARQ)
	@Produces(MediaType.APPLICATION_JSON)
	@Override
	public Response listar(@PathParam("emp") String emp, @PathParam("vend") String vend, @PathParam("destPath") String destPath) {
		//SigCdClsDao dao = new SigCdClsDao();
		//String retorno = gson.toJson(dao.listar(emp, vend));
		//Response response = Response.ok(retorno).build();
		
		SigCdClsDao dao = new SigCdClsDao();
		List<SigCdCls> sigcdclsList = dao.listar(emp, vend); 	
		File file = new File(destPath + File.separator + AtributosSIGCDCLS.NOME_TB_SIGCDCLS + Constantes.EXTENTION_ARQ_TRANSP);
		
		if (file.exists()) {
			file.delete();
		}
		
		try {
			file.createNewFile();
			JsonWriter writer = new JsonWriter(new FileWriter(file));
			
			writer.setIndent("  ");
			writer.beginArray();
			
			for (SigCdCls sigCdCls : sigcdclsList) {
				writer.beginObject();
				
				writer.name(AtributosSIGCDCLS.CODS).value(sigCdCls.getCods());
				writer.name(AtributosSIGCDCLS.DESCS).value(sigCdCls.getDescs());
				writer.name(AtributosSIGCDCLS.SITUAS).value(sigCdCls.getSituas());
				
				writer.endObject();
			}
			
			writer.endArray();
			writer.flush();
			writer.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		ResponseBuilder response = Response.ok(file);
		response.header(CONTENT_DISPOSITION, ANEXO + file.getName());
		
		return response.build();
	}

}
Ataxexe

me:
Exatamente, meu método gera um File e retorna através de um Response, então eu pego esse valor retornado para salvar em algum local no pc do cliente. Será que estou fazendo besteira?

Sim, está! O serviço não deveria salvar o arquivo no computador do cliente e, sim, o cliente pegar esse arquivo e salvar por si mesmo.

me1

Valew Ataxexe, eu alterei meu metodo para apenas retornar um file.

package br.com.fourcontrol.resources;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import com.google.gson.stream.JsonWriter;

import br.com.fourcontrol.dao.SigCdClsDao;
import br.com.fourcontrol.model.SigCdCls;
import br.com.fourcontrol.model.SigCdCls.AtributosSIGCDCLS;
import br.com.fourcontrol.util.Constantes;

@Path("/sigcdcls")
public class SigCdClsResource extends ResourceRodes {

	@GET
	@Path("/transp" + Constantes.EMP_VEND)
	@Produces(MediaType.APPLICATION_JSON)
	@Override
	public Response listar(@PathParam("emp") String emp, @PathParam("vend") String vend) {	
		SigCdClsDao dao = new SigCdClsDao();
		List<SigCdCls> sigcdclsList = dao.listar(emp, vend); 	
		File file = new File(AtributosSIGCDCLS.NOME_TB_SIGCDCLS + Constantes.EXTENTION_ARQ_TRANSP);
				
		try {
			JsonWriter writer = new JsonWriter(new FileWriter(file));
			
			writer.setIndent("  ");
			writer.beginArray();
			
			for (SigCdCls sigCdCls : sigcdclsList) {
				writer.beginObject();
				
				writer.name(AtributosSIGCDCLS.CODS).value(sigCdCls.getCods());
				writer.name(AtributosSIGCDCLS.DESCS).value(sigCdCls.getDescs());
				writer.name(AtributosSIGCDCLS.SITUAS).value(sigCdCls.getSituas());
				
				writer.endObject();
			}
			
			writer.endArray();
			writer.flush();
			writer.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		ResponseBuilder response = Response.ok((Object)file);
		response.header(CONTENT_DISPOSITION, ANEXO + file.getName());
		
		return response.build();
	}

}

Grato.

Criado 11 de setembro de 2013
Ultima resposta 11 de set. de 2013
Respostas 4
Participantes 2