简体   繁体   中英

upload a file in a servlet

I need to store cad files on my server, but I can't get the upload to work. I tried to get this working for hours but I just can't. Atm I have this. This gives me an error that

random number .temp doesn't exist ( 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";
    }
}

The temporary file representing the uploaded file could not be found.

The path indicates that it's stored in Glassfish work folder in Netbeans project folder. Perhaps there's some agressive cleanup/hotdeploy going on inside Netbeans and the temporary file was been swept too soon? Or perhaps there are just insufficient write rights? That depends on the context of how and where the exception is been thrown — which isn't clear from your question.

To start, I'd suggest telling @MultipartConfig to store the temp files in a different, a bit more permanent and writable, location . For example,

@MultipartConfig(location="/var/webapp/temp")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM