Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.34 KB | None | 0 0
  1. enter code here
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>BBDP</title>
  8. <link rel="Shortcut Icon" type="image/x-icon" href="img/frame/icon.ico">
  9. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  10. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  11. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  12. <link href="frame/metisMenu/metisMenu.css" rel="stylesheet">
  13. <script src="frame/metisMenu/metisMenu.js"></script>
  14. <link href="frame/sb-admin-2/sb-admin-2.css" rel="stylesheet">
  15. <script src="frame/sb-admin-2/sb-admin-2.js"></script>
  16. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  17. <!-- 醫生端框架 css -->
  18. <link href="css/doctorFrame.css" rel="stylesheet">
  19. <!-- HTML編輯器 -->
  20. <!-- Include external CSS. -->
  21. <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
  22. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.25.0/codemirror.min.css">
  23. <!-- Include Editor style. -->
  24. <link href="https://cdnjs.cloudflare.com/ajax/libs/froala-editor/2.5.1/css/froala_editor.pkgd.min.css" rel="stylesheet" type="text/css" />
  25. <link href="https://cdnjs.cloudflare.com/ajax/libs/froala-editor/2.5.1/css/froala_style.min.css" rel="stylesheet" type="text/css" />
  26. <!-- Include external JS libs. -->
  27. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.25.0/codemirror.min.js"></script>
  28. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.25.0/mode/xml/xml.min.js"></script>
  29. <!-- Include Editor JS files. -->
  30. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/froala-editor/2.5.1//js/froala_editor.pkgd.min.js"></script>
  31. <!-- languages -->
  32. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/froala-editor/2.5.1/js/languages/zh_tw.js"></script>
  33. </head>
  34. <body>
  35.  
  36. <!-- 右邊下面的HTML編輯器 -->
  37. <div id="HtmlEditor" class="panel-body" style="overflow-y: auto;">
  38. <div id="froala-editor"></div>
  39. </div>
  40.  
  41. <!-- HTML編輯器 -->
  42. <script>
  43. var url = "PatientInstructionServlet";
  44. var froalaUrl = "InstructionFroalaServlet";
  45. var maxInstructionID = 0;
  46.  
  47. //取得maxpatientInstructionID
  48. $(document).ready(function() {
  49. $.ajax({
  50. url : "PatientInstructionServlet",
  51. data : {
  52. state : "getMaxInstructionID"
  53. },
  54. dataType : "json",
  55. success : function(response) {
  56. maxInstructionID = response;
  57. froalaEdit();
  58. },
  59. error : function() {console.log("錯誤訊息");}
  60. });
  61. });
  62.  
  63. function froalaEdit(){
  64. var height = $("div.right-content").height() - $(".right-content > .panel-heading").height() - 120 - 50;
  65.  
  66. $(function() {
  67. $('div#froala-editor').froalaEditor({
  68. language: 'zh_tw',
  69. placeholderText: '請輸入文章內容...',
  70. height: height,
  71. //>= 1200px
  72. toolbarButtons: ['undo', 'redo', '|', 'fontFamily', '|', 'fontSize', 'color', '|', 'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', '-', '|', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', '|', 'specialCharacters', 'insertHR', 'quote', 'insertLink', 'insertTable', 'insertImage', 'insertVideo', '|', 'selectAll', 'clearFormatting', '|', 'fullscreen', 'help'],
  73. //>= 992px
  74. toolbarButtonsMD: ['undo', 'redo', '|', 'fontFamily', '|', 'fontSize', 'color', '|', 'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', '|', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', '|', 'specialCharacters', 'insertHR', 'quote', 'insertLink', 'insertTable', 'insertImage', 'insertVideo', '|', 'selectAll', 'clearFormatting', '|', 'fullscreen', 'help'],
  75. //>= 768px
  76. toolbarButtonsSM: ['undo', 'redo', '|', 'fontFamily', '|', 'fontSize', 'color', '|', 'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', '|', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', '|', 'specialCharacters', 'insertHR', 'quote', 'insertLink', 'insertTable', 'insertImage', 'insertVideo', '|', 'selectAll', 'clearFormatting', '|', 'fullscreen', 'help'],
  77. //< 768px
  78. toolbarButtonsXS: ['undo', 'redo', '|', 'fontFamily', '|', 'fontSize', 'color', '|', 'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', '|', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', '|', 'specialCharacters', 'insertHR', 'quote', 'insertLink', 'insertTable', 'insertImage', 'insertVideo', '|', 'selectAll', 'clearFormatting', '|', 'fullscreen', 'help'],
  79. fontFamilySelection: true,
  80. fontSizeSelection: true,
  81. fontSizeDefaultSelection: '30',
  82. fontSize: ['30', '36', '40', '48', '50', '55', '60', '72', '80', '84', '96', '100', '108', '120'],
  83. linkList: [{
  84. text: 'Google',
  85. href: 'http://google.com',
  86. target: '_blank',
  87. rel: 'nofollow'
  88. }],
  89. paragraphFormat: {
  90. N: 'Normal',
  91. H1: 'Heading 1',
  92. H2: 'Heading 2',
  93. H3: 'Heading 3',
  94. H4: 'Heading 4'
  95. },
  96. //設置視頻上傳參數。
  97. videoUploadURL: froalaUrl,
  98. videoUploadParams: { state: "uploadVideo",patientInstructionID: maxInstructionID},
  99. videoUploadMethod: 'POST',
  100. videoMaxSize: 50 * 1024 * 1024,
  101. videoAllowedTypes: [ 'webm' ,'mp4' ,'ogg' ] //允許上傳MP4,WEBM和OGG
  102. })
  103. /*********************************vedio*********************************/
  104. .on('froalaEditor.video.beforeUpload', function (e, editor, videos) {
  105. // Return false if you want to stop the video upload.//事件在開始上傳請求之前觸發,可用於更改上傳參數或取消操作
  106. console.log("beforeUpload");
  107. })
  108. .on('froalaEditor.video.uploaded', function (e, editor, response) {
  109. // Video was uploaded to the server.//事件在成功的視頻上傳請求之後,但在編輯器中插入視頻之前觸發
  110. console.log("uploaded");
  111. })
  112. .on('froalaEditor.video.inserted', function (e, editor, $video) {
  113. console.log("inserted");
  114. })
  115. .on('froalaEditor.video.replaced', function (e, editor, $img, response) {
  116. // Video was replaced in the editor.//將視頻替換為編輯器後觸發事件
  117. console.log("replaced");
  118. })
  119. .on('froalaEditor.video.beforeRemove', function (e, editor, $video) {
  120. console.log("beforeRemove");
  121. console.log("$video.attr('src') : " + $video.attr('src'));
  122. })
  123. .on('froalaEditor.video.removed', function (e, editor, $video) {
  124. console.log("removed");
  125. //deleteVideo(e, editor, $video); //刪除video
  126. console.log("刪除video");
  127. console.log("$video.attr('src') : " + $video.attr('src'));
  128. $.ajax({
  129. method: 'POST',
  130. url: froalaUrl,
  131. data: {
  132. state: "deleteVideo",
  133. src: $video.attr('src')
  134. }
  135. })
  136. .done (function (data) {
  137. console.log ('Vedio was deleted');
  138. })
  139. .fail (function (err) {
  140. console.log ('Vedio delete problem: ' + JSON.stringify(err));
  141. })
  142. });
  143. })
  144. }
  145.  
  146. </script>
  147.  
  148. </body>
  149. </html>
  150.  
  151. enter code here
  152.  
  153.  
  154. import java.io.File;
  155. import java.io.IOException;
  156. import java.io.InputStream;
  157. import java.io.PrintWriter;
  158. import java.nio.file.Files;
  159. import java.util.HashMap;
  160. import java.util.Map;
  161. import java.util.UUID;
  162.  
  163. import javax.servlet.ServletException;
  164. import javax.servlet.annotation.MultipartConfig;
  165. import javax.servlet.annotation.WebServlet;
  166. import javax.servlet.http.HttpServlet;
  167. import javax.servlet.http.HttpServletRequest;
  168. import javax.servlet.http.HttpServletResponse;
  169. import javax.servlet.http.HttpSession;
  170. import javax.servlet.http.Part;
  171.  
  172. import org.apache.commons.io.FilenameUtils;
  173. import org.apache.commons.lang.ArrayUtils;
  174.  
  175. import com.froala.editor.Image;
  176. import com.google.gson.Gson;
  177.  
  178. @MultipartConfig
  179. public class InstructionFroalaServlet extends HttpServlet {
  180. public InstructionFroalaServlet() {
  181. super();
  182. }
  183. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  184. response.setContentType("text/html;charset=UTF-8");
  185. Gson gson = new Gson();
  186.  
  187. String state = request.getParameter("state");
  188. HttpSession session = request.getSession();
  189. String doctorID = (String) session.getAttribute("doctorID");
  190. String patientInstructionID = request.getParameter("patientInstructionID");
  191.  
  192. /*******************************************************************************************/
  193.  
  194. String filePath = getServletContext().getRealPath("/");
  195. String InstructionFroalaPath = filePath + "Instruction\"+ doctorID+ "\" + patientInstructionID;
  196. File path = new File(InstructionFroalaPath);
  197.  
  198. System.out.println("state : " + state);
  199. System.out.println("filePath : " + filePath);
  200. System.out.println("InstructionFroalaPath : " + InstructionFroalaPath);
  201.  
  202. /*******************************************************************************************/
  203.  
  204. if (!path.exists()) { //如果路徑上沒有此資料夾,創一個
  205. System.out.println("!path.exists()");
  206. path.mkdirs();
  207. }
  208.  
  209. /*******************************************************************************************/
  210.  
  211. if(state.equals("uploadVideo")){
  212. System.out.println("uploadVedio");
  213.  
  214. // The route on which the file is saved.
  215. File uploads = new File(InstructionFroalaPath);
  216. System.out.println("uploads : " + uploads);
  217.  
  218. String multipartContentType = "multipart/form-data";
  219. String fieldname = "file";
  220. Part filePart = request.getPart(fieldname);
  221. System.out.println("filePart : " + filePart);
  222.  
  223.  
  224. // Create path components to save the file.
  225. Map < Object, Object > responseData;
  226. final PrintWriter writer = response.getWriter();
  227. String linkName = null;
  228.  
  229. try {
  230. System.out.println("try");
  231.  
  232. // Check content type.
  233. if (request.getContentType() == null ||
  234. request.getContentType().toLowerCase().indexOf(multipartContentType) == -1) {
  235.  
  236. throw new Exception("Invalid contentType. It must be " + multipartContentType);
  237. }
  238.  
  239. // Get file Part based on field name and also file extension.
  240. String type = filePart.getContentType();
  241. System.out.println("type : " + type);
  242. type = type.substring(type.lastIndexOf("/") + 1);
  243. System.out.println("type.substring : " + type);
  244.  
  245.  
  246. // Generate random name.
  247. String extension = type;
  248. extension = (extension != null && extension != "") ? "." + extension : extension;
  249. System.out.println("extension : " + extension);
  250. String name = UUID.randomUUID().toString() + extension;
  251.  
  252. System.out.println("name : " + name);
  253.  
  254. // Get absolute server path.
  255. String absoluteServerPath = uploads + name;
  256.  
  257. System.out.println("absoluteServerPath : " + absoluteServerPath);
  258.  
  259. // Create link.
  260. String vedioPath = request.getHeader("referer");
  261. //linkName = vedioPath + "files/" + name;
  262. String fileRoute = "Instruction/"+doctorID+"/"+patientInstructionID+"/";
  263. System.out.println("fileRoute : " + fileRoute);
  264. linkName = fileRoute + name;
  265.  
  266. System.out.println("vedioPath : " + vedioPath);
  267.  
  268.  
  269. // Validate file.
  270. String mimeType = filePart.getContentType();
  271. String[] allowedExts = new String[] {
  272. "mp4",
  273. "webm",
  274. "ogg"
  275. };
  276. String[] allowedMimeTypes = new String[] {
  277. "video/mp4",
  278. "video/webm",
  279. "video/ogg"
  280. };
  281.  
  282. if (!ArrayUtils.contains(allowedExts, FilenameUtils.getExtension(absoluteServerPath)) ||
  283. !ArrayUtils.contains(allowedMimeTypes, mimeType.toLowerCase())) {
  284.  
  285. System.out.println("if");
  286.  
  287. // Delete the uploaded file.
  288. File file = new File(absoluteServerPath);
  289.  
  290. if (file.exists()) {
  291. file.delete();
  292. }
  293.  
  294. throw new Exception("File does not meet the validation.");
  295. }
  296.  
  297. // Save the file on server.
  298. File file = new File(uploads, name);
  299.  
  300. try (InputStream input = filePart.getInputStream()) {
  301. System.out.println("try copy");
  302. System.out.println("input : " + input);
  303. System.out.println("file.toPath() : " + file.toPath().toString());
  304.  
  305. Files.copy(input, file.toPath());
  306. } catch (Exception e) {
  307. writer.println("<br/> ERROR: " + e);
  308. }
  309.  
  310. } catch (Exception e) {
  311. e.printStackTrace();
  312. writer.println("You either did not specify a file to upload or are " +
  313. "trying to upload a file to a protected or nonexistent " +
  314. "location.");
  315. writer.println("<br/> ERROR: " + e.getMessage());
  316. responseData = new HashMap < Object, Object > ();
  317. responseData.put("error", e.toString());
  318.  
  319. } finally {
  320. System.out.println("finally");
  321. responseData = new HashMap < Object, Object > ();
  322. responseData.put("link", linkName);
  323. System.out.println("linkName : " + linkName);
  324.  
  325. // Send response data.
  326. String jsonResponseData = new Gson().toJson(responseData);
  327. System.out.println("jsonResponseData : " + jsonResponseData);
  328.  
  329. response.setContentType("application/json");
  330. response.setCharacterEncoding("UTF-8");
  331. response.getWriter().write(jsonResponseData);
  332. }
  333. }
  334. if(state.equals("deleteVideo")){
  335. String src = request.getParameter("src");
  336. System.out.println("src : " + src);
  337.  
  338. File file = new File(filePath + src);
  339. System.out.println("filePath + src : " + filePath + src);
  340.  
  341. try {
  342. System.out.println("try delete");
  343. System.out.println("file.toPath() : " + file.toPath().toString());
  344.  
  345. Files.delete(file.toPath());
  346. } catch (Exception e) {
  347. e.printStackTrace();
  348. response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  349. return;
  350. }
  351. response.setContentType("application/json");
  352. response.setCharacterEncoding("UTF-8");
  353. response.getWriter().write(gson.toJson("Success"));
  354. }
  355.  
  356. }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement