formsubmitactionhandler.java
来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 380 行 · 第 1/2 页
JAVA
380 行
con.setDoOutput(true); if ((flags&(1<<5))!=0) { // XFDF boolean canonicalformat = (flags&(1<<9))!=0; submitXFDF(con, fields, canonicalformat); } else if ((flags&(1<<8))!=0) { // PDF submitPDF(con, docpanel.getPDF(), cancelbutton); } else if ((flags&(1<<2))!=0) { // HTML POST boolean coords = (flags&(1<<4))!=0; boolean canonicalformat = (flags&(1<<9))!=0; submitHTMLPost(con, fields, coords, canonicalformat); } else { // FDF boolean includeappendsaves = (flags&(1<<6))!=0; boolean includeannotations = (flags&(1<<7))!=0; boolean exclnonuserannots = (flags&(1<<10))!=0; boolean exclfkey = (flags&(1<<11))!=0; boolean embedform = (flags&(1<<14))!=0; boolean canonicalformat = (flags&(1<<9))!=0; submitFDF(con, docpanel.getPDF(), fields, includeappendsaves, includeannotations, exclnonuserannots, exclfkey, embedform, canonicalformat); } if (state==OK) { con.getOutputStream().close(); } } else { // HTML GET con.setRequestMethod("GET"); con.setDoOutput(false); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } // Now we've sent the request, read the response. The only valid response // we recognise is an FDF or XFDF, although there's no reason in principle // why we couldn't display something else (eg. dialog containing HTML response) // Object response = null; if (state==OK) { int status = con.getResponseCode(); if (status>=200 && status<=299) { String type = con.getContentType(); if ("application/vnd.fdf".equals(type) || "application/vnd.adobe.xfdf".equals(type)) { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (copyStream(con.getInputStream(), out)) { response = new FDF(new ByteArrayInputStream(out.toByteArray())); } } } else if (status>=400) { String enc = con.getContentEncoding(); if (enc==null) enc = "ISO-8859-1"; Reader in = new InputStreamReader(con.getErrorStream(), enc); StringWriter out = new StringWriter(); int c; while ((c=in.read())>=0) out.write(c); String msg = out.toString(); msg = msg.replaceAll("<head>.*</head>", ""); throw new SyncFailedException(msg); } } return response; } /** * Return the URL that should be used for a GET request. * @param url the URL * @param fields the fields to submit * @param coords whether to append co-ordinates (currently ignored) * @param canonicaldates whether to convert dates to canonical format (currently ignored) */ private URL createGetURL(URL url, Collection fields, boolean coords, boolean canonicaldates) throws IOException { String urlt = url.toString(); if (urlt.indexOf("#")>=0) urlt = urlt.substring(0, urlt.indexOf("#")); if (urlt.indexOf("?")>=0) urlt = urlt.substring(0, urlt.indexOf("?")); urlt += "?"; for (Iterator i = fields.iterator();i.hasNext();) { FormElement e = (FormElement)i.next(); String key = e.getForm().getName(e); String value = e==null ? "" : e.getValue(); urlt += URLEncoder.encode(key, "UTF-8"); urlt += '='; urlt += URLEncoder.encode(value, "UTF-8"); if (i.hasNext()) urlt += "&"; } return new URL(urlt); } /** * Submit the form as an HTTP POST * @param con the URLConnection * @param fields the fields to submit * @param coords whether to append co-ordinates (currently ignored) * @param canonicaldates whether to convert dates to canonical format (currently ignored) */ private void submitHTMLPost(HttpURLConnection con, Collection fields, boolean coords, boolean canonicaldates) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Writer out = new OutputStreamWriter(bout, "ISO-8859-1"); for (Iterator i = fields.iterator();i.hasNext() && state!=CANCELLED;) { FormElement e = (FormElement)i.next(); String key = e.getForm().getName(e); String value = e.getValue(); out.write(URLEncoder.encode(key, "UTF-8")); out.write('='); out.write(URLEncoder.encode(value==null?"":value, "UTF-8")); if (i.hasNext()) out.write('&'); } out.write('\n'); out.close(); if (state==OK) { con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("Content-Length", Integer.toString(bout.size())); copyStream(new ByteArrayInputStream(bout.toByteArray()), con.getOutputStream()); } } /** * Submit the form as an XFDF * @param con the URLConnection * @param fields the fields to submit * @param canonicaldates whether to convert dates to canonical format (currently ignored) */ private void submitXFDF(HttpURLConnection con, Collection fields, boolean canonicaldates) throws IOException { // TODO } /** * Submit the entire document as a PDF. * @param con the URLConnection * @param pdf the PDF */ private void submitPDF(HttpURLConnection con, PDF pdf, JButton cancelbutton) throws IOException { cancelbutton.setEnabled(false); ByteArrayOutputStream out = new ByteArrayOutputStream(); pdf.render(out); out.close(); cancelbutton.setEnabled(true); if (state==OK) { con.setRequestProperty("Content-Type", "application/pdf"); con.setRequestProperty("Content-Length", Integer.toString(out.size())); copyStream(new ByteArrayInputStream(out.toByteArray()), con.getOutputStream()); } } /** * Submit the form as an FDF. * @param con the URLConnection * @param PDF the PDF * @param fields the fields to submit * @param incappend if set, the submitted FDF file includes the contents of all incremental updates to the underlying PDF document, as contained in the Differences entry in the FDF dictionary. * @param annot if set, the submitted FDF file includes all mark-up annotations in the underlying PDF document. * param userannot if set, it includes only those markup annotations whose T entry matches the name of the current user, as determined by the remote server to which the form is being submitted. (The T entry for markup annotations specifies the text label to be displayed in the title bar of the annotation's pop-up window and is assumed to represent the name of the user authoring the annotation.) This allows multiple users to collaborate in annotating a single remote PDF document without affecting one another's annotations. * @param exclfkey if set, the submitted FDF excludes the F entry. * @param embed if set, the F entry of the submitted FDF is a file specification containing an embedded file stream representing the PDF file from which the FDF is being submitted. * @param canonicaldates whether to format dates in their canonical format */ private void submitFDF(HttpURLConnection con, PDF pdf, Collection fields, boolean incappend, boolean annot, boolean userannot, boolean exclfkey, boolean embed, boolean canonicaldates) throws IOException { FDF fdf = new FDF(pdf); fdf.setFields(fields); ByteArrayOutputStream out = new ByteArrayOutputStream(); fdf.render(out); out.close(); if (state==OK) { con.setRequestProperty("Content-Type", "application/vnd.fdf"); con.setRequestProperty("Content-Length", Integer.toString(out.size())); copyStream(new ByteArrayInputStream(out.toByteArray()), con.getOutputStream()); } } /** * Copy a Stream from the InputStream to the OutputStream. * Returns true if the whole stream completed, false if it was interrupted. */ private boolean copyStream(InputStream in, OutputStream out) throws IOException { byte[] buf = new byte[8192]; int len; while (state==OK) { len = in.read(buf, 0, buf.length); if (len<0 && state==OK) { state = DONE; } else if (len>0) { out.write(buf, 0, len); } } in.close(); out.flush(); if (state==DONE) { state = OK; return true; } else { return false; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?