'2024/08'에 해당되는 글 1건

HWP파일로 데이터를 다운로드 받고 싶어 하는 고객분이 계셔서

인터넷으로 관련 자료를 검색해보니 누군가 오픈소스로 공개해 놓았다. 

hwplib

https://github.com/neolord0/hwplib

 

GitHub - neolord0/hwplib: hwp library for java

hwp library for java. Contribute to neolord0/hwplib development by creating an account on GitHub.

github.com

인데 이것을 활용해서 테스트로만 만들어 본다. 

		<dependency>
		    <groupId>kr.dogfoot</groupId>
		    <artifactId>hwplib</artifactId>
		    <version>1.1.6</version>
		</dependency>

메이븐을 추가한다.

컨트롤러 소스 부분은 아래와 같다. 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import kr.dogfoot.hwplib.object.HWPFile;
import kr.dogfoot.hwplib.object.bodytext.Section;
import kr.dogfoot.hwplib.object.bodytext.paragraph.Paragraph;
import kr.dogfoot.hwplib.tool.blankfilemaker.BlankFileMaker;
import kr.dogfoot.hwplib.writer.HWPWriter;
/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/hwp")
public class Hwp_Controller {

	 
		@RequestMapping(value = { "/", "/download" }, method = { RequestMethod.GET, RequestMethod.POST})
	    public ResponseEntity<InputStreamResource> downloadHwpFile() {
	        try {
	            //HWPFile hwpFile = new HWPFile();
	            HWPFile hwpFile = BlankFileMaker.make( );


	            Section s = hwpFile.getBodyText( ).getSectionList( ).get( 0 );
				Paragraph firstParagraph = s.getParagraph( 0 );
				firstParagraph.getText( ).addString( "안녕하세요." );

	            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
	            HWPWriter.toStream(hwpFile, byteArrayOutputStream);
	            byte[] hwpBytes = byteArrayOutputStream.toByteArray();

	            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(hwpBytes);
	            InputStreamResource inputStreamResource = new InputStreamResource(byteArrayInputStream);

	            HttpHeaders headers = new HttpHeaders();
	            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=hwpFile.hwp");

	            return ResponseEntity.ok()
	                    .headers(headers)
	                    .contentLength(hwpBytes.length)
	                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
	                    .body(inputStreamResource);
	        } catch (Exception e) {
	            e.printStackTrace();
	            return ResponseEntity.status(500).build();
	        }
	    }
	
}

HWP파일이 생성되어 다운로드 되는것을 확인할수 있다.

블로그 이미지

은호아빠

여행, 맛집, 일상, 프로그래밍, 개발자, 윈도우, 웹, jsp, spring, db, mysql, oracle, c#

,