小编Uri*_*iel的帖子

将文件从REST Web服务发送到客户端的正确方法是什么?

我刚刚开始开发REST服务,但我遇到了一个困难的情况:从我的REST服务发送文件到我的客户端.到目前为止,我已经掌握了如何发送简单数据类型(字符串,整数等),但发送文件是另一回事,因为有太多的文件格式,我不知道我应该在哪里开始.我的REST服务是基于Java的,我使用的是Jersey,我使用JSON格式发送所有数据.

我读过有关base64编码的内容,有人说这是一种很好的技术,有人说这不是因为文件大小问题.什么是正确的方法?这就是我项目中的简单资源类的外观:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我猜测发送文件的代码是这样的:

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 …
Run Code Online (Sandbox Code Playgroud)

java rest json web-services jersey

94
推荐指数
3
解决办法
20万
查看次数

如何使用Java压缩/上传文件夹及其所有文件和子目录?

我正在阅读本文以了解如何使用Java压缩/解压缩文件.我用来指导我,它在压缩文件夹中的所有文件时效果很好,但是当我用一个包含更多文件夹的文件夹测试它时,它没有用,它引发了以下错误:

java.io.FileNotFoundException: assets (Access is denied) //assets is the name of the folder I tried to zip
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at Zip.main(Zip.java:24)
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的类,因为您将看到它与前一个链接中的相同的Code Sample 4:Zip.java类代码:

import java.io.*;
import java.util.zip.*;

public class Zip {
   static final int BUFFER = 2048;
   public void zip() {
      try {
         BufferedInputStream origin = null;
         FileOutputStream dest = new 
           FileOutputStream("H:\\myfigs.zip");
         CheckedOutputStream checksum = new 
           CheckedOutputStream(dest, new Adler32());
         ZipOutputStream out = new 
           ZipOutputStream(new 
             BufferedOutputStream(checksum));
         //out.setMethod(ZipOutputStream.DEFLATED);
         byte data[] …
Run Code Online (Sandbox Code Playgroud)

java compression directory file zipfile

1
推荐指数
1
解决办法
4624
查看次数

标签 统计

java ×2

compression ×1

directory ×1

file ×1

jersey ×1

json ×1

rest ×1

web-services ×1

zipfile ×1