该项目允许用户下载下载文件为压缩文件的文件。下载的压缩文件效果很好,只是空文件夹不会包含在压缩文件夹中。其中一位开发人员用他的MacBook使用下面的代码实现了它并没有触发任何错误。但是,当在ubuntu机器中执行源代码时,会发生错误。
代码:
def zipFolder(folderNamePath: String, subDirectory: String,zip: ZipOutputStream): Unit = {
val file = new File(folderNamePath)
val readBuffer = new Array[Byte](Buffer)
val fileList = file.list()
var i = 0
for( i <- 0 until fileList.length ){
val path = folderNamePath + "/" + fileList(i)
val currFile = new File(path)
if(currFile.isDirectory){
val filePath = currFile.getPath
zipFolder(filePath, subDirectory + '/' + fileList(i) ,zip)
}else{
val anEntry = new ZipEntry(subDirectory + '/' + fileList(i))
val bis = new BufferedInputStream(new FileInputStream(path), Buffer)
zip.putNextEntry(anEntry)
var bytesIn: Int = -1
while({
bytesIn = bis.read(readBuffer, 0, Buffer)
bytesIn != -1
}){
zip.write(readBuffer, 0, bytesIn);
}
bis.close()
}
}
//THE CODE BELOW IS TO ZIP EMPTY DIRECTORY
if(fileList.length == 0){
val path = folderNamePath
val anEntry = new ZipEntry(subDirectory)
val bis = new BufferedInputStream(new FileInputStream(path + "/"), Buffer)
zip.putNextEntry(anEntry)
bis.close()
}
}
记录错误:
! @75akh0a6a - Internal server error, for (POST)
[/storage_ws/download_directory_file] ->
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution
exception[[FileNotFoundException: /home/jarvis/project/storage-api/media/jarvis/test/Test/Testing (Is a directory)]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
at play.filters.cors.AbstractCORSPolicy$$anonfun$2.applyOrElse(AbstractCORSPolicy.scala:146)
at play.filters.cors.AbstractCORSPolicy$$anonfun$2.applyOrElse(AbstractCORSPolicy.scala:145)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.io.FileNotFoundException: /home/jarvis/project/storage-api/media/user/jarvis/Test/Testing (Is a directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at tools.ZipUtils$.zipFolder(ZipUtils.scala:122)
at tools.ZipUtils$$anonfun$zipFolder$1.apply$mcVI$sp(ZipUtils.scala:101)
at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:160)
at tools.ZipUtils$.zipFolder(ZipUtils.scala:96)
at tools.IO$.handleDownloadDirectoryFile(IO.scala:176)
at controllers.FileHandleController$$anonfun$downloadDirectoryFile$1$$anonfun$apply$50.apply(FileHandleController.scala:586)
代码执行良好,没有以下情况if(fileList.length == 0){
用于压缩空文件夹,压缩文件夹将排除空文件夹。
答案 0 :(得分:1)
问题发生的原因是您尝试在目录而不是文件上打开FileInputStream。
确实,您在此处看到了异常消息:
Caused by: java.io.FileNotFoundException: /home/jarvis/project/storage-api/media/user/jarvis/Test/Testing (Is a directory)
at java.io.FileInputStream.open0(Native Method)
在这里,您将使用稍微修改过的方法来解决问题。请注意,我替换了所有&#39; /&#39;改为使用File.separator。
def zipFolder(folderNamePath: String, subDirectory: String,zip: ZipOutputStream): Unit = {
val file = new File(folderNamePath)
val readBuffer = new Array[Byte](Buffer)
val fileList = file.list()
var i = 0
for( i <- 0 until fileList.length ){
val currFile = new File(folderNamePath, fileList(i))
if(currFile.isDirectory){
val filePath = currFile.getPath
zipFolder(filePath, subDirectory + File.separator + fileList(i) ,zip)
}else{
val path = folderNamePath + File.separator + fileList(i)
val anEntry = new ZipEntry(subDirectory + File.separator + fileList(i))
val bis = new BufferedInputStream(new FileInputStream(path), Buffer)
zip.putNextEntry(anEntry)
var bytesIn: Int = -1
while({
bytesIn = bis.read(readBuffer, 0, Buffer)
bytesIn != -1
}){
zip.write(readBuffer, 0, bytesIn);
}
bis.close()
}
}
//THE CODE BELOW IS TO ZIP EMPTY DIRECTORY
if(fileList.length == 0){
zip.putNextEntry(new ZipEntry(subDirectory + File.separator))
zip.closeEntry()
}
}
只有在解压缩压缩文件时才会显示空文件夹。 至少,对于Ubuntu默认的zip工具(Archive Manage)