我正在创建一个程序,它是来自服务器的下载文件
public void downloadFile(String fileURL, String saveDir, String user, String pass, String FileName)
throws IOException {
String authString = user + ":" + pass;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestProperty("Authorization", "Basic " + authStringEnc);
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
}
所以当我使用参数下载文件时我遇到了错误 输出是
Content-Type = application/http
Content-Disposition = null
Content-Length = 3217551
fileName = 13.25.00-13.26.00[R][0@0][0].dav
Exception in thread "main" java.io.FileNotFoundException: C:\Cam\13.25.00-13.26.00[R][0@0][0].dav
(The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
at dynakode.utility.Downloader.downloadFile(Downloader.java:65)
at dynakode.camera.DynakodeCamera.cennection(DynakodeCamera.java:50)
at dynakode.camera.DynakodeCamera.main(DynakodeCamera.java:16)
Java Result: 1
你可以看到错误是文件名,目录名或卷标语法不正确所以如何解决这个问题
我正在更改此行
String saveFilePath = saveDir + "\\" + "test.dav";
我手动给名字而不是文件下载到驱动程序
输出
Content-Type = application/http
Content-Disposition = null
Content-Length = 3217551
fileName = 13.25.00-13.26.00[R][0@0][0].dav
File downloaded
这是我想要的
问题已修复
@TAsk你写的是Space有问题但是在C:/ cam ..这是来自服务器的文件名有空格" 13.25.00-13.26.00[R][0@0][0].dav"
这就是创建问题
所以我修剪所有路径
saveFilePath = saveFilePath.trim();
使用此问题修复了
答案 0 :(得分:1)
你的字符串在路径中有\13
,它将其视为字符表示。
C:\Cam\13.25.00-13.26.00[R][0@0][0].dav
^
您应该将其更改为/
或\\
所以改变你的saveFilePath
String saveFilePath = saveDir + "\\" + fileName;
String saveFilePath = saveDir + "/" + fileName;
这不应该是因为File.separator()
(由user2864740
指出),所以根据我的另一个原因导致这个问题,我认为这是C:
之前的额外空间或者在路径的某个地方。
" C:\Cam"+File.separator+"13.25.00-13.26.00[R][0@0][0].dav"
^//<-------Extra Space you can use string.trim() to remove extra spaces
修改强>
我觉得目录(Cam
)的最后一件事不存在,可能是造成这种情况的最后一个选项。
答案 1 :(得分:0)
因为您指定的路径与预期的路径不同,所以它会抛出java.io.FileNotFoundException
确保您已在cam
驱动器
c
目录