我剛剛開(kāi)始開(kāi)發(fā)REST服務(wù),但是遇到了一個(gè)困難的情況:將文件從REST服務(wù)發(fā)送到客戶端。到目前為止,我已經(jīng)掌握了如何發(fā)送簡(jiǎn)單數(shù)據(jù)類型(字符串,整數(shù)等)的竅門(mén),但是發(fā)送文件是另一回事,因?yàn)橛刑辔募袷?,我什至不知道從哪里開(kāi)始。我的REST服務(wù)是在Java上進(jìn)行的,并且我正在使用Jersey,我正在使用JSON格式發(fā)送所有數(shù)據(jù)。我已經(jīng)讀過(guò)有關(guān)base64編碼的信息,有人說(shuō)這是一種好技術(shù),而其他人則說(shuō)這不是因?yàn)槲募笮?wèn)題。正確的方法是什么?這是我項(xiàng)目中的簡(jiǎn)單資源類的外觀:import java.sql.SQLException;import java.util.List;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.Context;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Request;import javax.ws.rs.core.UriInfo;import com.mx.ipn.escom.testerRest.dao.TemaDao;import com.mx.ipn.escom.testerRest.modelo.Tema;@Path("/temas")public class TemaResource { @GET @Produces({MediaType.APPLICATION_JSON}) public List<Tema> getTemas() throws SQLException{ TemaDao temaDao = new TemaDao(); List<Tema> temas=temaDao.getTemas(); temaDao.terminarSesion(); return temas; }}我猜發(fā)送文件的代碼是這樣的:import java.sql.SQLException;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;@Path("/resourceFiles")public class FileResource { @GET @Produces({application/x-octet-stream}) public File getFiles() throws SQLException{ //I'm not really sure what kind of data type I should return // Code for encoding the file or just send it in a data stream, I really don't know what should be done here return file; }}我應(yīng)該使用哪種注釋?我見(jiàn)過(guò)有人建議@GET使用@Produces({application/x-octet-stream}),這是正確的方法嗎?我要發(fā)送的文件是特定的文件,因此客戶端不需要瀏覽文件。誰(shuí)能指導(dǎo)我如何發(fā)送文件?我是否應(yīng)該使用base64對(duì)其進(jìn)行編碼以將其
從REST Web服務(wù)向客戶端發(fā)送文件的正確方法是什么?
慕桂英3389331
2019-11-25 12:38:37