0

我需要将cad文件存储在我的服务器上,但无法上传。我试图让这个工作几个小时,但我就是做不到。自动取款机我有这个。这给了我一个错误

随机数.temp 不存在( java.io.FileNotFoundException: /home/nick/.netbeans/7.1.2/config/GF3_1/domain1/generated/jsp/lrt/upload_700679fb_13a96c78523__7ffb_00000006.tmp (No such file or directory )

package Admin;

import java.io.IOexception;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet(name = "UploadServlet", urlPatterns = {"/UploadServlet"})
@MultipartConfig
public class UploadServlet extends HttpServlet {

    @Override
     protected void doPost(HttpServletRequest request, 
       HttpServletResponse responst) throws ServletException, IOException {
      Collection<Part> parts = request.getParts();
      if (parts.size() != 3) {
         //can write error page saying all details are not entered
      }
       Part filePart = request.getPart("cad");
       InputStream sInputStream = filePart.getInputStream();
       //read imageInputStream
       filePart.write(filePart.getName());
       //Read Name, String Type 
       Part namePart = request.getPart("cad");
       if(namePart.getSize() > 20){
           //write name cannot exceed 20 chars
       }
       //use nameInputStream if required        
       InputStream nameInputStream = namePart.getInputStream();
       //name , String type can also obtained using Request parameter 
       String nameParameter = request.getParameter("name");
       //Similarly can read age properties
       Part agePart = request.getPart("age");
       int ageParameter = Integer.parseInt(request.getParameter("age"));
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
4

1 回答 1

2

找不到代表上传文件的临时文件。

该路径表明它存储在 Netbeans 项目文件夹中的 Glassfish 工作文件夹中。也许 Netbeans 内部正在进行一些激进的清理/热部署,并且临时文件被扫描得太早了?或者也许只是写权限不足?这取决于抛出异常的方式和位置的上下文 - 从您的问题中不清楚。

首先,我建议告诉@MultipartConfig将临时文件存储在一个不同的、更永久和可写的location中。例如,

@MultipartConfig(location="/var/webapp/temp")
于 2012-10-25T12:34:04.110 回答