كيفية إدراج ملف خارج التطبيق (الحرب) باستخدام jsp تشمل

StackOverflow https://stackoverflow.com/questions/828973

  •  06-07-2019
  •  | 
  •  

سؤال

أنا باستخدام "jsp:وتشمل" إدراج ملف ثابت في واحدة من بلدي ملفات jsp.أنه يعمل بشكل جيد عندما html ثابتة الملف الموجود داخل مجلد التطبيق.ومع ذلك إذا أبقى خارج مجلد التطبيق لا يتم تضمينه في الملف JSP.

ملاحظة:لقد خلق سياق المجلد حيث ثابتة حفظ الملف و أنا قادرة على عرض ملف html مع توجيه url.

الرجاء المساعدة..

هل كانت مفيدة؟

المحلول

لقد حل هذه المشكلة باستخدام ج:استيراد الوسم.

تحديد URL الديناميكية لقد استخدمت الفول:تعريف الوسم.شكرا على الاقتراحات والمساعدة.

نصائح أخرى

ويمكنك فقط استخدام jsp:include للموارد داخل إطار تطبيق الويب الخاص بك. سوف تحتاج إلى استخدام إما java.io.File أو ما شابه ذلك لتحميل من مسار نظام الملفات، أو <وأ href = "http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ClassLoader.html #getResource (java.lang.String) "يختلط =" نوفولو noreferrer "> ClassLoader.getResource لتحميل مورد من CLASSPATH.

وفائدة إضافية تتمثل في طريقة <c:import> هو أنه يمكنك تعيين الترميز باستخدام السمة charEncoding. لا يمكنك أن تفعل هذا مع البيانات إما <%@include%> أو <jsp:include>.

أنا باستخدام فئة أن يكون وسيلة للحصول على المحتوى من رابط:مثلا: http://link.inet.vn/seo-website/inet.html

public class URLReader
{
    public URLReader()
    {
        in = null;
        out = null;
        requestType = null;
        headers = null;
        content = null;
        headers = new Hashtable();
    }

    public void doGet(String server, String uri, int port)
    {
        try{
            Socket client = new Socket(server, port);
            client.setKeepAlive(true);
            in = new DataInputStream(client.getInputStream());
            out = new DataOutputStream(client.getOutputStream());
            out.writeBytes("GET " + uri + " HTTP/1.0\r\n");
            out.writeBytes("Host: " + server + "\r\n");
            out.writeBytes("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n");
            out.writeBytes("Accept-Language: en-us\r\n");
            out.writeBytes("Accept-Encoding: gzip, deflate\r\n");
            out.writeBytes("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n");
            out.writeBytes("Connection: Keep-Alive\r\n");
            out.writeBytes("Content-Length: 0\r\n\r\n");
            out.flush();
            parseRequest();
            out.close();
            in.close();
            client.close();        
        }catch(Exception e){
            System.out.println(e.getMessage());
        }

        return;
    }

    public byte[] getContent()
    {
        return content;
    }

    public String getHeader(String name)
    {
        String key = (String)headers.get(name);
        return key;
    }

    public Hashtable getHeaders()
    {
        return headers;
    }

    public String getRequestType()
    {
        return requestType;
    }

    public static void main(String args[]) throws IOException
    {
        URLReader reader = new URLReader();

        reader.doGet("link.inet.vn", "/seo-website/inet.html", 80);
        if(reader.getContent() != null)
            System.out.println(new String(reader.getContent(),"UTF-8"));

    }

    private boolean parseRequest()
    {
        byte match[];
        int index;
        String line;
        match = (new byte[] {
            13, 10, 13, 10
        });
        index = 0;
        line = "";
        int i;
        try{
            while((i = in.read()) >= 0) 
            {
                if(i == match[index] && index <= 3)
                    index++;
                else
                    index = 0;
                if(index == 4)
                {
                    content = readHTTPContent();
                    break;
                }
                line = line + (char)i;
                if(line.length() > 2 && i == 10)
                {
                    int pos = line.indexOf(':');
                    if(pos != -1)
                    {
                        String name = line.substring(0, pos);
                        String value = line.substring(pos + 1, line.length()).trim();
                        setHeader(name, value);
                    } else
                    {
                        setRequestType(line.substring(0, line.length()).trim());
                    }
                    line = "";
                }
            }

            return true;
        }catch(Exception e){
            System.out.println(e.getMessage());
            return false;
        }                
    }

    private byte[] readHTTPContent()
        throws IOException
    {
        ByteArrayOutputStream baosContent = new ByteArrayOutputStream();
        int contentLength = 0;
        try {
            contentLength = Integer.parseInt( (String) headers.get("content-length"));
        } catch (Exception ex) {
            contentLength = 1024 * 1024;
        }
        int bytesToRead = 0;
        int bytesRead   = 0;
        int totalBytesRead = 0;
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        if (contentLength < bufferSize) {
            bytesToRead = contentLength;
        } else {
            bytesToRead = bufferSize;
        }
        do {
            try {
                bytesRead = in.read(buffer, 0, bytesToRead);
            } catch (InterruptedIOException e) {
                /* comms read timeout expired, no problem */
                System.out.println("Timeout reading from socket");
            }
            if (bytesRead == -1) {
                in.close();
               // throw new IOException("Connection was closed by client.");
                break;
            } else if (bytesRead > 0) {
                //////////////////////////////////////
                baosContent.write(buffer, 0, bytesRead);
                //////////////////////////////////////
                totalBytesRead += bytesRead;
            }
            // Left bytes to read
            if (contentLength - totalBytesRead > bufferSize) {
                bytesToRead = bufferSize;
            } else {
                bytesToRead = contentLength - totalBytesRead;
            }
        } while (totalBytesRead < contentLength);

        return baosContent.toByteArray();        
    }


    public void saveToFile(byte data[], String filename)
    {
        try{
            File f = new File(filename);
            FileOutputStream fout = new FileOutputStream(f);
            fout.write(data);
            fout.close();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }        
        return;
    }


    private void setHeader(String key, String value)
    {
        headers.put(key.toLowerCase(), value);
    }

    private void setRequestType(String s)
    {
        requestType = new String(s);
    }

    private byte content[];
    private Hashtable headers;
    private DataInputStream in;
    private DataOutputStream out;
    private String requestType;
}

وفقط من باب المصلحة - ما هو السبب لأنه يريد أن يفعل هذا؟ - قد يكون هناك نهج بديل

وأظن أن كنت تريد أن يكون بعض التكوين التي هي مستقلة عن ملف WAR، وهي فريدة من نوعها لكل بيئة أن WAR يتم نشرها ل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top