开发者

how to save a file in http server using jsf?

String urlPath = "http://hostname:portno/filepath.pdf";
            url = new URL(urlPath);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            OutputStream outStream = conn.getOutputStream();
            out = new Bu开发者_StackOverflowfferedOutputStream(outStream);
            if (byteContent != null && byteContent.length > 0) {
                out.write(byteContent);
                byteLength = byteContent.length;
            }

this code doesn't give any exception but file is not created in my tomcat location


This is not the way to write files to tomcat. You are making an HTTP request which sends the bytes to the server.

You should create a FileOutputStream(..) and write the bytes there instead. This is in case your code is running in Tomcat.

If your code is a separate application, you need to create a servlet that can handle the incoming request and save the file. No direct way to simply push files to the server (and for the better - otherwise everyone will be uploading files)


apache commons-fileupload

  FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);
    Iterator<FileItem> iterator = items.iterator();
    while(iterator.hasNext()) {
        FileItem item = iterator.next();
        if(!item.isFormField()) {
            File uploadedFile = new File("fullPathToFile");
            System.out.println(uploadedFile.getAbsolutePath());
            item.write(uploadedFile);                                        
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜