save.jsp

来自「applet在線上繪圖,允許在瀏覽器上繪圖,最後儲存成各式的圖檔」· JSP 代码 · 共 57 行

JSP
57
字号
<%@ page import="java.io.*" %>
<%@ include file="base64.jsp" %>
<%
/*--------------------------------------------------------------------
This script accepts the image from the hidden field 'image'.
The image data are BASE64 encoded. The script decodes it
to binary byte array and saves it to a disk file.

The text notes (field "notes") are saved to the file "notes.txt"
---------------------------------------------------------------------*/

// Image and notes are saved in the script's directory,
// but you can assign any directory, for example: saveDir = "mydir";

File saveDir = new File(application.getRealPath(request.getServletPath())).getParentFile();
String imageFilename  = "image.png";
String notesFilename  = "notes.txt";

    // save the image
    String base64EncodedImage = request.getParameter("image");
    byte imageBytes[] = Base64Decode(base64EncodedImage);
    OutputStream os = new FileOutputStream(new File(saveDir,imageFilename));
    try {
	os.write(imageBytes);
    }
    finally {
	os.close();
    }	

    // save the text notes
    String notes = request.getParameter("notes");    
    PrintWriter writer = new PrintWriter(new FileOutputStream(new File(saveDir,notesFilename)));
    try {
	writer.print(notes);
    }
    finally {
	writer.close();
    }    
%>

<html>

<head>
    <title>J-Painter. JSP Example 2</title>
</head>

<body>

<p><img src="<%=imageFilename%>" border=1>
<br>Image is saved to <%=imageFilename%></p>

<p>Notes are saved to <a href="<%=notesFilename%>" target="_blank"><%=notesFilename%></a></p>

</body>

</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?