Skip to content Skip to sidebar Skip to footer

Upload Pdf File as Binary File in Java

Use JavaScript to upload binary files such as PDF and Excel to ABAP server and parse them

JerryWang_ Wang Zixi 2021-12-06 xi:49:02

This is a Jerry 2022 In the first 71 An commodity , It's also the official account of Wang Zixi 348 Original articles .

Jerry I published an article before Don't use any framework , Handwritten pure JavaScript Upload local files to ABAP The server , Subsequently that, many friends left messages , The questions raised tin exist summarized into the post-obit two categories :

(1) Customer laissez passer multipart/form-data Data sent in format ,ABAP Except like Jerry This paper adopts string parsing, which is a cumbersome way to deal with external , Is there any other fashion ?

(2) Can I upload binary files, such as Excel To ABAP And analyze ?


This article will reply these 2 questions .

Use JavaScript adopt multipart/form-data Format transport PDF and Excel File to ABAP The server

Near multipart/form-data Detailed clarification of the format , Reference resources Mozilla Develop communities and W3 Org Documents :

  • https://programmer.mozilla.org...
  • https://www.w3.org/html/wg/sp...

I'll make some modifications on the ground of the previous example , stay Form The 2 types used in are file Of input label , Upload separately PDF and Excel file :


Local for testing PDF file :PDF.pdf, The size is 30129 byte .


The contents are as follows :

Local for testing Excel file :Exam.xlsx, The contents are every bit follows :

Click on HTML Folio upload file hyperlink , stay Chrome Developer tools observe HTTP POST The requested load , Contains PDF and Excel Two input Command contains a binary stream (stream):

Click on view source, see multipart/course-data Data details :

We can nevertheless Chrome The upload was observed in the developer tool PDF and Excel And Content-Blazon I.e. certificate blazon . Different from the previous example of uploading a text file , Y'all can't see the binary contents of the two files here —— These binary contents tin be found in ABAP Observed in the server-side debugger .


To upload PDF File as an example , stay ABAP Received by the server form-data data , As shown in the effigy below , The dark-green highlighted area is the higher up figure Chrome The file name that tin can be observed in the developer tool PDF.pdf And file types awarding/pdf, and %PDF-1.iv# At the beginning , Namely PDF The binary content of the file .


Accurately speaking ,PDF The format is a mixture of text and binary streams . Open with text editor PDF.pdf, You can run into that the file header contains the file metadata described by text characters , For example, the creation and modification time of the file , The name of the tool that created the file, then on , The second one-half is the binary stream .

In that location are already many open source tools, such equally JavaScript Libraries tin exist used to generate and parse PDF The file , Interested friends can search in the search engine Jerry These articles :

  • Use ABAP and JavaScript Code generation PDF Several means to file
  • Use JavaScript Relieve the current folio as PDF, Support the saving of pictures and text
  • PDF How to turn a certificate into markdown Format

For uploading to ABAP Server's PDF File name of the file , We still analyze in the same way as the previous commodity , Extract... From the cord in the red rectangle in the figure below .

For the green highlighted in the above figure PDF Binary data of ,CL_HTTP_REQUEST Corresponding methods are provided to extract . The key code is shown in the figure below :


When ABAP The format of client data received by the server is multipart/form-data when , call CL_HTTP_REQUEST Of num_multiparts Method tin can get parts The number of , Reuse get_multipart Method , Pass in each office The index of , You tin can go this part An case of .

Call the... Of the reference get_content_type and get_data Method , You can parse the blazon of uploaded file ( such as pdf The format corresponds to awarding/pdf) And binary content .

Called at this point SAP CRM Attachment creation API Iii parameters of : file name , The file blazon and binary contents of the file are ready , phone call API You lot tin can upload PDF and Excel data , Create to become SAP CRM Attachments to the sales order .

Create good PDF and Excel Fastened at SAP CRM The system displays the following :

Open both attachments , Brand sure that later on uploading , Its content is completely consistent with the local file :

How to employ ABAP Parse the uploaded Excel file

This topic , Actually Jerry 2022 Manufactures from Use ABAP functioning Excel Several ways to Has been systematically introduced .

We are ABAP Observed in the debugger , The local extension is xlsx Of Excel file , Upload to ABAP Server time , Its content-type by :
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

What is? openxmlformats? The following is a specific case to illustrate .

With my local Excel File as an example , Modify the extension from xlsx Change to cipher, Then decompress :

Discover out xlsx The file is really a compressed package , After unzipping, a folder is generated , It contains several subfolders and files as shown in the figure below .

Upper figure Excel The file has a name Sheet1 The content folio of ,A1 The value is ABAP,B1 The value is Coffee, This information is maintained in the extracted worksheets Subfolders of the binder sheet1.xml Inside :

Highlighted above XML c Nodes represent Prison cell,r="A1" and r="B1", On behalf of these ii cell Where Row ID,c Child nodes of v Contains Jail cell The specific value of .

Information technology'southward not hard to notice out ,sheet1.xml There is no direct reference to ABAP and Coffee Including the literal value of the cord , And just store its index ,0 and 1. done Java Friends of development , This design tin be compared to Java String constant puddle .

At that place is another file in the extracted binder sharedStrings.xml, seeing the proper name of a thing one thinks of its function , Maintained Excel worksheets All strings that appear in , Used in sheets To share . Each individual sheet xml The file only maintains the index of the cord used , To reduce the Excel The size of the document .

therefore , As long as I'm familiar TEST.xlsx Rename information technology to Exam.null And the purpose of each file generated later on decompression , namely Open up XML Formats Protocol specification for , You tin can use whatsoever high-level programming language to parse Excel file .

Tin can be in WikiPedia Found in Open XML Formats The function of each file divers by the protocol :

https://en.wikipedia.org/wiki...

SAP CRM A tool grade is provided , be based on Open XML Formats analysis Excel The contents of the document :cl_xlsx_document.

Just put the Excel The binary content of the file is passed in , The tool class returns a Excel References to documents , According to the various Get Method , You lot can access Excel The document consists of Open XML Formats The content of each part of the protocol definition .

The core logic is shown in the figure below , The code is self describing , No more details here .

Of course , Open source projection abap2xlsx Some other option :

https://github.com/sapmentors...

as for SAP Fiori Awarding passed SAP Gateway Upload the technical details of the attachment ,Jerry In the future, nosotros volition introduce .

This article involves the front and back-terminate complete source code , Here, please link Download .

Thank you for reading .

Jerry Of ABAP projection

  • Jerry Of ABAP, Java and JavaScript Stew
  • ABAP What should developers learn in the future
  • Jerry 2022 May Day vacation in :8 A classic sort algorithm ABAP Realization
  • Jerry Of ABAP A drove of original technical articles
  • 300 That'due south ok ABAP The code implements the simplest blockchain prototype
  • Use Java+SAP Cloud platform +SAP Cloud Connector call ABAP On-Premise Functions in the system
  • stay SAP Cloud platform CloudFoundry Consumption in the environment ABAP On-Premise OData service
  • ABAP vs Java, Breaststroke vs gratis style
  • Chat C Language and ABAP
  • Apply it past hand ABAP Channel Develop some gadgets , Improve the efficiency of daily work
  • I utilize ABAP The boring things I've done
  • Don't similar SAP GUI? Try it on Eclipse Conduct ABAP Develop
  • Use Visual Studio Lawmaking Write and activate ABAP Code
  • Yours ABAP Has the program given light to Buddha ? To take a try Jerry This lilliputian pull a fast one on
  • stay SAP Cloud platform ABAP Write the first paragraph in the programming surround ABAP Program
  • SAP Officially released ABAP Programming specification
  • ABAP Lawmaking Inspector Those hidden features , Practise you know ?
  • It's still in use ABAP Behave SAP Second development of products ? Allow's understand this new concept of secondary evolution
  • ABAP Netweaver The parasitic programming languages in the body
  • from SAP A blog in the community started , Chat SAP The feelings behind the product naming
  • In the clouds ABAP Restful Service development
  • How to be in SAP Cloud platform ABAP In the programming environment CDS view Exposed as OData service
  • Use abapGit stay ABAP On-Premises Systems and SAP Cloud platform ABAP Code transfer betwixt environments
  • 30 Minutes Restful ABAP Programming The model evolution supports the addition, deletion, modification and query Fiori application
  • Jerry Let'south testify you Restful ABAP Programming Model series two :Activity and Validation The implementation of the
  • Jerry Let's prove you Restful ABAP Programming Model series three : Cloud ABAP Application debugging
  • SAP On the cloud platform ABAP How to swallow tertiary-party services in programming environs
  • ABAP It's time for developers to become to the cloud - Now you tin can employ it for costless SAP Cloud platform ABAP A trial version of the environment
  • Learning without thinking is useless - SAP Cloud platform ABAP The origin and applicative scenarios of programming surroundings
  • SAP Trident application in cloud platform
  • How to base of operations on Restful ABAP Programming Model development and deployment of a support to add, delete, change and query Fiori application
  • SAP 2022 TechEd Key Note Reading : Under the cloud age SAP How to do secondary development for employees ?
  • What are they? ABAP Keywords and Syntax , here we are ABAP You lot can't apply it in the cloud environment ?
  • ABAP Finally, the evolution surround supports automatic formatting with hump naming ABAP Variable name
  • utilize ABAP 740 New keywords for REDUCE Finish a practical job
  • A period of shivering ABAP Code
  • Yesterday'southward Halloween ABAP Monster code puzzle , The answer is published
  • To introduce a new method in the field of ABAP The method of efficient copy of inner tabular array in kernel manner
  • Use SAP Deject Application Programming Model development OData A practical example of
  • When ABAP Encounter Prometheus
  • Use ABAP Draw scalable vector
  • ABAP Evolution environment syntax highlights things
  • SAP Seven weapons of error message debugging : So that all error messages can exist located
  • Apply ABAP functioning Excel Several means to
  • SAP GUI Favorite transaction lawmaking management tool in
  • SAP GUI and Windows The registry
  • With Debug Authority can exercise bad things ? Be conscientious , Your every motility is monitored by the system
  • ABAP CCDEF, CCIMP, CCMAC, CCAU, CMXXX What the hell are these things
  • Realization ABAP Three ways of provisional breakpoints
  • Employ SAT Tracking monitoring opens from the browser SAP Application performance and phone call stack
  • One 13 year ABAP Veteran's advice : Know the basics , Yes ABAP Development is profitable without damage
  • SAP ABAP Netweaver Containerization , Impossible tasks ?
  • SAP Product enhancement technology review
  • SAP API A complete set up of development methods
  • Talking about Coffee and SAP ABAP Static agent and dynamic agent , also every bit ABAP An effort at aspect oriented programming
  • SAP ABAP Awarding server HTTP Response status code (Status Lawmaking)
  • SAP ABAP In existence Java List This kind of tool course ?CL_OBJECT_COLLECTION Get to know
  • ABAP Interview questions series : Writing a group will result in a deadlock (Deadlock) Of ABAP Plan
  • SAP ABAP Netweaver The standard login mode of the server is explained
  • SAP ABAP Keyword syntax and ABAP Code generation tool Lawmaking Composer
  • SAP ABAP SM50 Alternative use of - ABAP Detection of database table read operations by the working process
  • Most SAP ABAP Graphic symbol variable and string variable grapheme number of a noesis point , And a encarmine example
  • SAP ABAP A gear up of keywords IS Spring, IS Not INITIAL and IS ASSIGNED An analysis of the usage of
  • SAP ABAP and Java The weak reference in (WeakReference) And soft quotes (SoftReference)
  • SAP AMDP Introduce - ABAP managed HANA Database process
  • Give you lot the ABAP Objects are labeled (Tag)
  • Today in history : In programming linguistic communication null The billion dollar quote is incorrect
  • ABAP Development Tool Code templates and other practical tips
  • SAP ABAP Development Tool Ten tips to improve development efficiency
  • How to be in SAP BTP platform ABAP Consumption in programming environment is based on Lather Of Web Service
  • ABAP Will information technology really be out of date ? Chat ABAP By , Present and time to come
  • be based on abapGit and abaplint Of ABAP An example of continuous integration
  • Don't use whatsoever framework , Handwritten pure JavaScript Upload local files to ABAP The server

more Jerry The original article of , All in :" Wang Zixi ":

saucedoserow1952.blogspot.com

Source: https://javamana.com/2021/12/20211206114858122n.html

Postar um comentário for "Upload Pdf File as Binary File in Java"