java http zip参数,如何从Web API 2 HttpGet发送zip文件

我试图弄清楚如何从给定的文件夹路径创建新的zip文件,并将其发送回发件人 .

需要的是该文件将在请求它的发件人处下载 . 我已经看到了很多答案,但没有人帮我确切的答案 .

我的代码:

Guid folderGuid = Guid.NewGuid(); string folderToZip = ConfigurationManager.AppSettings [“folderToZip”] folderGuid.ToString();

Directory.CreateDirectory(folderToZip);

string directoryPath = ConfigurationManager.AppSettings [“DirectoryPath”]; string combinedPath = Path.Combine(directoryPath,id);

DirectoryInfo di = new DirectoryInfo(combinedPath); if(di.Exists){//提供zip文件的路径和名称以创建字符串zipFile = folderToZip“\”folderGuid“.zip”;

//call the ZipFile.CreateFromDirectory() method

ZipFile.CreateFromDirectory(combinedPath, zipFile, CompressionLevel.Fastest, true);

var result = new HttpResponseMessage(HttpStatusCode.OK);

using (ZipArchive zip = ZipFile.Open(zipFile, ZipArchiveMode.Read))

{

zip.CreateEntryFromFile(folderFiles, "file.zip");

}

var stream = new FileStream(zipFile, FileMode.Open);

result.Content = new StreamContent(stream);

result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")

{

FileName = "file.zip"

};

log.Debug("END ExportFiles()");

return ResponseMessage(result);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用Java的压缩库来将文件打包成zip文件,并将其发送给前端。下面是一个简单的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipFileSender { public static void main(String[] args) { String filePath = "path_to_file"; String zipFilePath = "path_to_zip_file"; File fileToZip = new File(filePath); try { FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); addToZip(fileToZip, fileToZip.getName(), zos); zos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } private static void addToZip(File file, String fileName, ZipOutputStream zos) throws IOException { FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); } } ``` 在上面的示例中,我们创建了一个`ZipFileSender`类,它将提供的文件(`filePath`)打包为一个zip文件(`zipFilePath`)。我们使用`java.util.zip.ZipOutputStream`类来创建zip文件并添加文件zip文件中。`addToZip`方法用于将文件添加到zip文件中。最后,我们使用`FileOutputStream`类将zip文件发送给前端。运行以上代码,将会在指定路径生成名为`zipFilePath`的zip文件,你可以将其发送给前端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值