gpt4 book ai didi

java - 将 VARBINARY 转换为 PDF

转载 作者:行者123 更新时间:2023-11-30 15:53:08 24 4
gpt4 key购买 nike

在 SQL 中,我有一个名为“数据”的字段,其中包含曾经是 PDF 的内容,但已转换为 VARBINARY(MAX) 字段。我正在从 java servlet 中提取该数据,我需要知道如何对其进行格式化,以便将其发送到前端并显示为 PDF。我见过许多与 BASE 64 编码\解码相关的解决方案,但这些解决方案并没有被证明是有用的。以下是将数据放入 SQL 后的示例:

0x255044462D312E350D0A25B5B5B5B50D0A312030206F626A0D0A3C3C2F547970652F436174616C6F672F50616765732032203020522F4C616E6728656E2D555329203E3E0D0A656E646F626A0D0A322030206F626A0D0A3C3C2F547970652F50616765732F436F756E7420312F4B6964735B2033203020525D203E3E0D0A656E646F626A0D0A332030206F626A0D0A3C3C2F547970652F506167652F506172656E742032203020522F5265736F75726365733C3C2F466F6E743C3C2F46312035203020522F46322037203020523E3E2F50726F635365745B2F5044462F546578742F496D616765422F496D616765432F496D616765495D203E3E2F4D65646961426F785B2030203020363132203739325D202F436F6E74656E74732034203020522F47726F75703C3C2F547970652F47726F75702F532F5472616E73706172656E63792F43532F4465766963655247423E3E2F546162732F533E3E0D0A656E646F626A0D0A342030206F626A0D0A3C3C2F46696C7465722F466C6174654465636F64652F4C656E67746820323737373E3E0D0A73747265616D0D0A789C955A4B6FE4C611BE0BD07F9823279068369BCFA39D788304D985836C72117CA038DCD100323922C5C5FEFCD4ABBB8BD4702CC1C07AD88FEAEA7A7E55AD5FBEDEDEFCF4C9ECD2244EB2DDD76FB7376697C07F3062ABB8B4BB3231B1AD765FFFB8BD497647FCE7EFB7370FD1E7666CBBE7DDE77D1635E3A9DF57D1B0FF7DF7F59FB737BF02C55FBE063AB6B4715A6A3A0FD14EAD3548B54CE2BCCA764551C5F92E83AF3ADF997C3776B737DFFEC2F47EFA94EE0C2C2B165C96699CA4B011B704261FA27F0D63B73769F4C7FEBE84E3EE6D1D9DF6F75974DE1B134D33FEABA60EC3DE24D1332E18461C2AA38996BFEE7359D2F0161B75AF70E73B3FDE0EFDD4B5AF305A46B3EC6D0E482E9C47BFA6D6FFEC8F38CFFB3B38358FFC61B127FCEBFE3EA5219A6B561C7FC76F90FC8CAB26184BABE81B521A81453E04571C4F4080EEF5BC2FA206D66549D48D0D721BEE40C4981175A917244D926AF0CA22A999AF566CEB30B79B3A5C6B2DCF975A23466662E43CE3A1747287D7ABE9D043CC9CC06DFF01ECF73C43DB06DA46BBCE8E61588612ECBCF0481DA4E234DF3A4D8E3045F4050547120822D47A00F29D9F45F6C45C707C216318988643F38CE427C5B3577B2377647D1751DFA2E20EC4601F2EF3327BC39A026956DF84CB68EA71F6D6BC3074B851B08C4DF5D96D175CAB2F5B39DD791EE72926DB405BF902BF40BC62806B0B9EE609475ABCEEF9844A08CE464AE39D406262B325DE0FFC9BC65BBCB19B6D07A4D61FE73DF8D446284A2B1BD7A5E2FB41F9DB7FC8DCA733CAAEEB0F7B0918D3D4F10124B5012D2E03AEE711B80FA7F7A82E0E1981E2CF48B1EB8960431BD3B7A6BAF22F71D1E17082A1E1AAA2D2F7FB59BAF2B349794437EE8D25F3484A0821B9B211654D935C0B17FD5745C6BA006207096738A962C969B9AEEF5AB5AEFBC197A38F4D5BC0C9E0139E27E5188AAFBF2A8BF73722C203B913EB539821990F2FC8D18C1AAE2B1045DF70D035A5BBFBA4591635C1763CA039F6DE4878F44056705569C9FBBDCBACBDAB1939C99C3A48B72E2AE4C4CB409F1D04051924FD35530B3C9B9C13531A720331DA86C886669C1121F45F21B1725F5C4119A6097A4037640128D3CFD9F0FDDC0B29AB41413AF797907CE4A0D504B60F9EB40E1779F0FD623BA0AB3003C91F2E5EB8F0CB7184366DA1941C62825132DFC62879FD6EBFCBEB95DF11CBC4E1343514F193E83C905AF1D32686B9FCE172330E105870B64723A8940E5548FAE008739049AF63B2438A4F38ECDC72E9957E669D88CC92559F75254C7A8F0FA47B600366C0AD5C9A99640739F5346BFDE370D3CAFE96032B47DC82D79014C8A6953F6FE8A37CB74BE5D5CAA5D89E89B5961D9E85E620406E17C10EBF97580947484833271E1CCA520E0FC8BDC76F2E5DE30676B990BD7007C0E999A68D3E416D7AEA904B72EBC308317BA9485CD18D03305EBACFDF20483C3514BF3AEFC5FE3C2147A4B237A456E9D5E48B8480BF9EEE8288BA795337858DB3322887B4704D41C53A51CD0A369F1796CA8046A017FC730CA8077D0942F8716CBE636E23E048631036070CDAB1DFFF6FF83553C6995E280F989A719ED40FCEEF0C923863525639A1D840EF97A06308691B655251C7A656125846A0654595C6D62C963EB10F1E46E2793CBDDE81F69913C152F70A4C99F292D71BA920BAF9E8F25A136E1F6E4059BA2799D1F71DA6CB4D0BB025F22A169016B129AE1A400EEB161610144C760746BE031BEF49D97D8B4989113C5E63C45C476B89C78EB29A46E872DD3BC953DFFC1DE696EEF3E86266A6A25AC56270312D0F994D73B42A21ABE0FA1AB730DB2C55C87668790558CD869EAB32AE8D96C995AC94A64ACE7F1206D32C2E8D16B2605FE413401068FFC02E41E19F32429AFAD2EF9E73814D2F25155795C0ACC475C09A35142ECD2B0E838D2D0AEB34352AD33B67C351AD557661AF53A06E956F8535A8782F6B0443A670CC342DCD847A68A2637BB95B00F7A975F0924410B371138DFF51EA239978E53E4A0D992F4B2D5C2E3439586C682D29DFAFB5A45E69ED4D0FC0948533F7E28D4A705241F5B0F43E5378EA62E2E5D50B3DC781E827D0AF84690A2385826BF8E16AA3C26D18427B842A1C45EB33264EAE13008C4F40592648256F02305E89F478C633BA9EC604B6B474BC60D66599EE648556F3C45AA223670744715F730DC767F5FBDD2DABD7EEB6464C2EB8484594B32A9CC44451740F5F9F053B476DCC63ECE33DF5455E5DE03B7623656941E1C1AD7971C8EAD3B33F2E442E09F7CB8AEBCE27B975B558A06E76041CFBAD989699D85A2D9307CE502A2D2DB1CF02F1DBE8ACD32FC1FAD65BEB7429FF62928020DB0EFD77DCD8782CE42C417D3E4230C70CBAA9F6F2FDFE9A9517FD7599734BBA938BA939445F9F4582634A6D176AD5F262F4833B76A17AFB9B772FA8BB690F07C071E29AC08567CCF5B3F35D9635162415FC2B18C2E998E655B502A2FF41FF7B0CD05081D47959F0318D5832DF6FAE1C1D4ED2C62BB81FC2C52116860D42B7695313C5071CB0583BE0C491A4EBEF7C450F676286AAAD6EA316D2AAF5C5AE879775297517972C56F721BE28B0E7FB10CD1286B03B7A74090B24C2F9E3A4EF22B3CA51FDAFBB7024A1DBC4B5027B899FAF2750AEA340FB42A0773D0D5F87438C682621332D2C55DFC077C0D57D49994FDC70D0F65B08A4D8D461F6016FCAD6DEC4717DC42EDCE5CC75BF44A59AA849CBB8D4341F04D0868B4B129F9446D05C0CC3962C5B410686CBAD4703A102B6E9C2C70919A1178A796B5A7892C8154759AED4389942020B7D5DB75BF99F9401CEA7351CE9E86EAF813C9742F2F1F671C4BAA9037170D600F9AA56ED073CD3AE3D33B41D42FC9BB87974706D08631340ACD5C566AD377259C7F79A96D84FE63C4A3A7AB53577E1884E35DBFD568AA8846CACC188C5081397935227D74B75471F1C41A82B21C4A03F52F09F8404699E02B4499345092F97ECF8B074B572D9B9280228DA504A52C4892EC36C72B510CF4C1D57562BA6EB4F7BF134573EE9D7889DA4A1A7810B3317BC72DD6B76FDC13F2B37B2CBC5538020480432110A1B57FAD59CBDB0FADA8258DBB55612A756DF7BBBD6B2B551C2BC6EE1B6B671B6B07081B8680A3D9B41B50C1015DF74AB5369306A559AF0832B3B256103059D6D0A5194AFAEAA90D0D39A9D1E1F6EF053B270785B4288D7CCED85AC846F5A74CCD137EC5F75E0736DC0B5F3C59EF69B3C09630A5F8447B242D4398EF28899E293ED338286C1215FD8EBDD25ADDDAAD011E0B0763572D9B278BF5ECB6AA5D720D34CDAEF33414FCC0EE791DE874FEA81420273269D86C12B4CBD60FAE21356AD7AEAA486664D4A5E1D1784A936D5347D02E07A56DE0D8BCD67435CDA7A1E51552A120E72614EC4C3249F0ECBBD658A401501DA112162AA430A57EA5755547CC0F58AB5EBF9F6B23C7A71D8F1ADF5D446FD8C6FB4A1D785C33657C85D1BA2A4235CE0F16CD371C7D53DCFE0E4F2AD43910D92F1CB7CE7DF2AD88D1FFC8E05B9B896CCA80951C7B2F595110F39FCFF4821199963FFBC1C56B22CC3669312DA03B9F1F2FA5CB8A88BBFAB27B2DDBCB7D9075C2E5BBBDC5690DFF15F5648C59BEBF44DEFC726043D4074143EECC517F9CC15A3571A24FE41FB59414079167E74CD98266014F52725AD13641D7DE2268EEFA2C0E6EBBD6F772071D15213400EBDFCD710E1595AB7BCB90AB9861CACFD80C7591BE7F66D507CE3EE3B0C6CDEFA7329BEAAC5728C278B22D294820E970F2B015470F4CF9C438BDD764FD46475D01C61D30E619BC2250ED050A5AC3BC6EA918DD44EE47B92D7CBDC6D61892CADC9958234DEF6F2E9AF99D24B7FCA64F189B8BCB0EDFF5747508E0D0A656E6473747265616D0D0A656E646F626A0D0A352030206F626A0D0A3C3C2F547970652F466F6E742F537562747970652F54727565547970652F4E616D652F46312F42617365466F6E742F417269616C2C426F6C642F456E636F64696E672F57696E416E7369456E636F64696E672F466F6E7444657363726970746F722036203020522F4669727374436861722033322F4C61737443686172203131342F576964746873203130203020523E3E0D0A656E646F626A0D0A362030206F626A0D0A3C3C2F547970652F466F6E7444657363726970746F722F466F6E744E616D652F417269616C2C426F6C642F466C6167732033322F4974616C6963416E676C6520302F417363656E74203930352F44657363656E74202D3231302F436170486569676874203732382F4176675769647468203437392F4D6178576964746820323632382F466F6E74576569676874203730302F58486569676874203235302F4C656164696E672033332F5374656D562034372F466F6E7442426F785B202D363238202D3231302032303030203732385D203E3E0D0A656E646F626A0D0A372030206F626A0D0A3C3C2F547970652F466F6E742F537562747970652F54727565547970652F4E616D652F46322F42617365466F6E742F417269616C2F456E636F64696E672F57696E416E7369456E636F64696E672F466F6E7444657363726970746F722038203020522F4669727374436861722033322F4C61737443686172203132302F576964746873203131203020523E3E0D0A656E646F626A0D0A382030206F626A0D0A3C3C2F547970652F466F6E7444657363726970746F722F466F6E744E616D652F417269616C2F466C6167732033322F4974616C6963416E676C6520302F417363656E74203930352F44657363656E74202D3231302F436170486569676874203732382F4176675769647468203434312F4D6178576964746820323636352F466F6E74576569676874203430302F58486569676874203235302F4C656164696E672033332F5374656D562034342F466F6E7442426F785B202D363635202D3231302032303030203732385D203E3E0D0A656E646F626A0D0A392030206F626A0D0A3C3C2F417574686F72284D617263656C204D6172696E6F29202F43726561746F7228FEFF004D006900630072006F0073006F0066007400AE00200057006F007200640020003200300031003029202F4372656174696F6E4461746528443A32303136303730373039333831322D30342730302729202F4D6F644461746528443A32303136303730373039333831322D30342730302729202F50726F647563657228FEFF004D006900630072006F0073006F0066007400AE00200057006F007200640020003200300031003029203E3E0D0A656E646F626A0D0A31302030206F626A0D0A5B2032373820302030203020302030203020302030203020302030203020302030203020302030203020302030203020302030203020302030203020302030203020302030203020302030203020302030203020302030203020302030203833332030203020302030203020302030203020302030203020302030203020302030203020302030203535362030203535362030203535362030203020302032373820302030203237382030203631312036313120302030203338395D200D0A656E646F626A0D0A31312030206F626A0D0A5B20323738203020302030203020302030203020302030203020302032373820302032373820302030203020302030203020302030203020302030203020302030203020302030203020363637203020373232203732322036363720363131203020302032373820302030203535362038333320373232203020363637203737382030203636372030203732322036363720302030203020302030203020302030203020302035353620353536203530302035353620353536203237382035353620353536203232322030203020323232203833332035353620353536203535362035353620333333203530302032373820353536203530302030203530305D200D0A656E646F626A0D0A31322030206F626A0D0A3C3C2F547970652F585265662F53697A652031322F575B2031203420325D202F526F6F742031203020522F496E666F2039203020522F49445B3C33454531373636413535414137333435413738303236393235343731373330323E3C33454531373636413535414137333435413738303236393235343731373330323E5D202F46696C7465722F466C6174654465636F64652F4C656E6774682035353E3E0D0A73747265616D0D0A789C63600082FFFF1981A420030388F283506D608A31154CF17482295E5D30C52704A1368229FE89604AA0184C09BA3030000034DB067B0D0A656E6473747265616D0D0A656E646F626A0D0A787265660D0A302031330D0A3030303030303030303020363535333520660D0A30303030303030303137203030303030206E0D0A30303030303030303738203030303030206E0D0A30303030303030313334203030303030206E0D0A30303030303030333537203030303030206E0D0A30303030303033323039203030303030206E0D0A30303030303033333733203030303030206E0D0A30303030303033363032203030303030206E0D0A30303030303033373631203030303030206E0D0A30303030303033393835203030303030206E0D0A30303030303034323131203030303030206E0D0A30303030303034343230203030303030206E0D0A30303030303034363937203030303030206E0D0A747261696C65720D0A3C3C2F53697A652031332F526F6F742031203020522F496E666F2039203020522F49445B3C33454531373636413535414137333435413738303236393235343731373330323E3C33454531373636413535414137333435413738303236393235343731373330323E5D203E3E0D0A7374617274787265660D0A343935310D0A2525454F460D0A787265660D0A3020300D0A747261696C65720D0A3C3C2F53697A652031332F526F6F742031203020522F496E666F2039203020522F49445B3C33454531373636413535414137333435413738303236393235343731373330323E3C33454531373636413535414137333435413738303236393235343731373330323E5D202F5072657620343935312F5852656653746D20343639373E3E0D0A7374617274787265660D0A353336360D0A2525454F46

我已经到了可以显示页数正确的 PDF 的地步,但每一页都是完全空白的。任何帮助将不胜感激。数据库表示数据类型为“application/pdf;filename="DocName.pdf";frevvo-attachment=true; charset=utf-8",转换可以在 Java 或 Javascript 上进行。

更新:

因此,感谢 Mark 的帮助,我能够让它工作,但也有一些异常(exception)。在一些我得到以下错误:

线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:43677 在 java.lang.String.charAt(String.java:658) 在 pdftestapp.PDFTestApp.hexStringToByteArray(PDFTestApp.java:40) 在 pdftestapp.PDFTestApp.main(PDFTestApp.java:31)

这是我的代码:

package pdftestapp;
import java.io.*;
import java.util.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
*
* @author mmarino
*/
public class PDFTestApp {

/**
* @param args the command line arguments
*/


public static void main(String args[]) throws Exception{
String str = "0x255044462D312E350..."; //data omitted

str = str.substring(2)

byte[] arr = hexStringToByteArray(str1);
Files.write(Paths.get("test.pdf"), arr);
}

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}

最佳答案

测试它:

如果你得到了 String 表示,使用这样的东西:

import java.nio.file.Files;
import java.nio.file.Paths;
public class Main {
public static void main(String args[]) throws Exception{
String str = "255[...]0A2525454F46"; //0x removed, omitted data
byte[] arr = hexStringToByteArray(str);
Files.write(Paths.get("test.pdf"), arr);
}

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}

您的 pdf 文件将能够读取:

decoded pdf

如果您的 pdf 是字节数组,您可以将其直接写入文件。

要做到这一点:

如果问题是如何使用浏览器向用户显示它,您应该做类似的事情:

@WebServlet("/foo.pdf")
public class PdfServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] file = getsomehow();
response.setHeader("Content-Type", "Your staffs content type");
response.setHeader("Content-Length", file.length);
response.setHeader("Content-Disposition", "inline; filename=\"something.pdf\"");
response.getOutputStream().write(file);
response.getOutputStream().close();
}

}

关于java - 将 VARBINARY 转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39003372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com