gpt4 book ai didi

java - 浏览时找不到文件

转载 作者:行者123 更新时间:2023-11-29 04:03:14 24 4
gpt4 key购买 nike

我有一个问题是上传文件。它在我的计算机上运行良好,但在部署到服务器时失败。

系统是浏览文件,然后系统将文件压缩后再上传到服务器。当客户端浏览文件时,服务器会产生文件未找到的错误。这是我的代码:

  try {
//This is a code to read a zipfile.
String dir = request.getParameter("dirs");
System.out.println(dir);
String tmp = dir.replace( '\\', '/' );
System.out.println(tmp);
String inFilename = tmp;
// String inFilename = dir;
String outFilename = "c:/sms.zip";
//String outFilename = "/webapps/ROOT/sms.zip";
FileInputStream in = new FileInputStream( inFilename);
ZipOutputStream out = new ZipOutputStream(
new FileOutputStream(outFilename));

// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(inFilename));

byte[] buf = new byte[1024];
int len;

while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
//End of zipping file.

//Start uploading.
SimpleFTP ftp = new SimpleFTP();

// Connect to an FTP server on port 21.
ftp.connect("xxxxx", 21, "xxx", "xxxx");

// Set binary mode.
ftp.bin();

// Change to a new working directory on the FTP server.
ftp.cwd("web");

// Upload some files.
ftp.stor(new File("sms.zip"));

ftp.disconnect();
//finish uploading
out.closeEntry();
out.close();
in.close();
response.sendRedirect("../BakMeClient/success.jsp");
}
catch (IOException e) {
System.out.println(e);
}

String dir 是文件的位置。错误信息是:

java.io.FileNotFoundException: D:\RELIVA\listmenu.java (The system cannot find the file specified)


感谢您的所有评论。根据我的观察,问题是这个脚本是在服务器上而不是客户端上运行的。

我的意思是假设您浏览文件,例如 c:/test.txt。当你点击上传按钮时,表单会将路径发送给服务器,服务器会在自己的目录中查找路径,当然找不到。

我希望你明白发生了什么。那么现在:如何让它在客户端读取路径?

最佳答案

这里肯定有问题:

// Upload some files.
ftp.stor(new File("sms.zip"));

存档已在 c:/sms.zip 创建,但您尝试从相对文件位置 sms.zip 读取它( 等于 ${JAVA_HOME}/sms.zip 如果我没记错的话 正确的部分在 Joachim 的评论中,谢谢!!)。

将这些行替换为

// Upload some files.
ftp.stor(new File("c:/sms.zip"));

如果这没有帮助,那么在使用 FTP 发送文件之前另外尝试关闭 ZipOutputStream。有可能 ZIP 文件尚未在文件系统上创建,只是因为流仍处于打开状态。

关于java - 浏览时找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2047797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com