上传图片和音频到DB

jsp页面

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="/pages/exercise/editExerciseFrame.jsp"%>
<script language= javascript  type= text/javascript  src="<c:url value='/default/scripts/jquery.form.js'/>" ></script><style type="text/css">#container{
clear:both;
width:100%;
height:400px;
}/***一下为新添加***/
.font-16{font-family: 微软雅黑;font-size: 16px;font-weight: bold;font-style: normal;text-decoration: none;color: #999999;
}table#table2 tr{height: 45px;
}table#table2 tr td{
/**border: 1px solid #888;**/
}table#table2 tr td:FIRST-CHILD{text-align: right;
}table#table2{border-collapse:collapse;border-spacing:0;}
.speaker {background-position: -90px 0px;
}.icon-button {display: inline-block;zoom: 1;vertical-align: baseline;margin: 0 2px;outline: none;cursor: pointer;padding: 0px;width: 20px;height: 20px;border: solid 0px #fff;background: url('<c:url value="/images/control_all_btn.png"/>') no-repeat scroll 0px 0px;
}
</style><!-- 中间独立的部分 --><script type="text/javascript">
//根据题目序号获取中间部分内容
$.getMiddleContent = function(index,itemObj){var content = $("#middleContent").clone();content.attr("id", "middleContent" + index);if(itemObj != null){var question = itemObj.question;var audioObj = content.find(".speaker");audioObj.attr("audioId", question)audioObj.css("visibility", "visible");var answerArray = itemObj.answer.split(",");for(var i=0; i<answerArray.length; i++ ){var imageId = answerArray[i].split("_")[1];content.find("#img_" + (i+1)).attr("src","<s:url value='/imagehandler/getImageById.action?id='/>"+imageId);content.find("#img_" + (i+1)).attr("imageId",imageId);}}//播放器触发content.find(".speaker").click(function(){var audioId = $(this).attr("audioId");var audioType = $(this).attr("audioType");if (audioId) {var audioUrl = "audio/" + audioId + "." + audioType;thisMovie("editExerciseAudioPlayer").loadAndPlayAudio(audioUrl);}});return content;};
//检查数据完整性
$.checkEditContent = function(){return true;
};//获取用户编辑后的信息
$.getEditContent = function(){var itemArray  = new Array();$(".exercise_item_container").each(function(){var sentenceArray  = new Array();//question保存音频IDvar question = $(this).find(".speaker").attr("audioId");var answer = "";//answer保存(序号_图片ID)for( var i=1; i<=6; i++ ){var imgObj = $("#img_" + i);if( imgObj.attr("imageId") == "" ){break;}answer = answer + i + "_" + imgObj.attr("imageId")+",";}answer = answer.substring(0, answer.length-1);if(question!=""&&answer!=""){itemArray.push( {"question": question, "answer": answer});}			});return itemArray;
};//选择上传文件后回调
$.onFileSelected = function(file){var filePath = $(file).val();if(filePath==null || filePath== ''){alert('请选择要上传的音频文件');return;}var speaker = $(file).nextAll(".speaker");var info = $(file).next("span");info.text("上传中...");$(file).parents("form").ajaxSubmit({dataType: "json",success: function (data){if (data.success) {var audioId = data.audioId;var audioType = data.audioType;var audioName = data.audioName;info.text("");speaker.attr("audioId", audioId);speaker.attr("audioType", audioType);speaker.css("visibility", "visible");} else {info.text("上传失败,请重新上传");}}});
};//选择上传文件后回调
$.onImgFileSelected = function(file){var filePath = $(file).val();if(filePath==null || filePath== ''){alert('请选择要上传的图片文件');return;}var image = $(file).parents("td").next("td").find("img");var info = $(file).next("span");info.text("上传中...");$(file).parents("form").ajaxSubmit({dataType: "json",success: function (data){var imageId = data.imageId;if (imageId !="") {image.attr("src","<s:url value='/imagehandler/getImageById.action?id='/>"+imageId);image.attr("imageId",imageId);info.text("");} else {info.text("上传失败,请重新上传");}},error:function(){info.text("上传失败,请重新上传");}});
};$().ready(function(){$("input .icon-button").hover(function(){$(this).attr("background-position", "-90px -30px")},function(){$(this).attr("background-position", "-90px 0px")});});//flash播放器加载完回调
function onPlayerCompleted(){//与流媒体服务器建立连接var rtmpServerUrl = "${rtmpServerUrl}";console.info(rtmpServerUrl);thisMovie("editExerciseAudioPlayer").setRtmpServerURL(rtmpServerUrl);
}function thisMovie(movieName) {if (navigator.appName.indexOf("Microsoft") != -1) {return window[movieName];}else {return document[movieName];}
}</script><!-- 隐藏的ActionScript播放器类  -->
<object style="width: 1px; height: 1px;position: absolute; top:0px; left:0px;" type="application/x-shockwave-flash" data="<c:url value='/flash/NetStreamAudioPlayerAS.swf?onLoadCompleted=onPlayerCompleted'/>" name="editExerciseAudioPlayer" id="editExerciseAudioPlayer" ><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="<c:url value='/flash/NetStreamAudioPlayerAS.swf?onLoadCompleted=onPlayerCompleted'/>" /><param name="quality" value="high" /><param name="scale" value="noScale" /><param name="wmode" value="transparent" />
</object><div style="display: none;"><div id="middleContent"><div><span class="font-16">上传录音且按顺序上传3-6张相关图片!</span></div><table id="table2" style="margin-left: 20px;margin-top: 10px;"><tr><td>音频文件:</td><td><form action="<c:url value='/audiohandler/uploadByAjax.action'/>" method="post" enctype="multipart/form-data"><input id="uploadAudio" name="file" accept="audio/mp3" type="file" οnchange="$.onFileSelected(this)"><span class="font-12"></span><input class="icon-button speaker" audioId="" audioType="mp3" type="button" style="visibility: hidden;"></form></td><td></td></tr><tr><td>1.</td><td><form id="01" action="<s:url value='/admin/uploadImage.action'/>" method="post" enctype="multipart/form-data"><input id="uploadImg_01" name="file" accept="image/*" type="file" οnchange="$.onImgFileSelected(this)"><span class="font-12"></span></form></td><td><img id="img_1" class="wordImg"  width="45px" height="45px" imageId=""/></td></tr><tr><td>2.</td><td><form id="02" action="<s:url value='/admin/uploadImage.action'/>" method="post" enctype="multipart/form-data"><input id="uploadImg_02" name="file" accept="image/*" type="file" οnchange="$.onImgFileSelected(this)"><span class="font-12"></span></form></td><td><img id="img_2" class="wordImg"  width="45px" height="45px" imageId=""/></td></tr><tr><td>3.</td><td><form id="03" action="<s:url value='/admin/uploadImage.action'/>" method="post" enctype="multipart/form-data"><input id="uploadImg_03" name="file" accept="image/*" type="file" οnchange="$.onImgFileSelected(this)"><span class="font-12"></span></form></td><td><img id="img_3" class="wordImg"  width="45px" height="45px" imageId=""/></td></tr><tr><td>4.</td><td><form id="04" action="<s:url value='/admin/uploadImage.action'/>" method="post" enctype="multipart/form-data"><input id="uploadImg_04" name="file" accept="image/*" type="file" οnchange="$.onImgFileSelected(this)"><span class="font-12"></span></form></td><td><img id="img_4" class="wordImg"  width="45px" height="45px" imageId=""/></td></tr><tr><td>5.</td><td><form id="05" action="<s:url value='/admin/uploadImage.action'/>" method="post" enctype="multipart/form-data"><input id="uploadImg_05" name="file" accept="image/*" type="file" οnchange="$.onImgFileSelected(this)"><span class="font-12"></span></form></td><td><img id="img_5" class="wordImg"  width="45px" height="45px" imageId=""/></td></tr><tr><td>6.</td><td><form id="06" action="<s:url value='/admin/uploadImage.action'/>" method="post" enctype="multipart/form-data"><input id="uploadImg_06" name="file" accept="image/*" type="file" οnchange="$.onImgFileSelected(this)"><span class="font-12"></span></form></td><td><img id="img_6" class="wordImg"  width="45px" height="45px" imageId=""/></td></tr></table></div>
</div>


图片上传Action

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts><package name="imagehandler" namespace="/imagehandler" extends="web-default"><!--根据id获取图片--><action name="getImageById" class="imageHandlerAction" method="getImageById"><result name="success" type="stream">  <param name="contentType">image/jpeg</param>  <param name="inputName">imageStream</param>  <param name="bufferSize">2048</param>  </result></action><!-- 上传图片--><action name="uploadByAjax" class="imageHandlerAction" method="uploadByAjax"><result name="ajax" type="jsonmessage"><param name="message">message</param></result></action></package></struts>

图片上传类

package com.wisemen.hhb.module.imagehandler.action;import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;import net.sf.json.JSONObject;import org.apache.commons.io.FileUtils;import com.wisemen.framework.server.action.BaseAction;
import com.wisemen.framework.util.StringUtils;
import com.wisemen.hhb.module.base.domain.Image;
import com.wisemen.hhb.module.imagehandler.service.IImageHandlerService;public class ImageHandlerAction extends BaseAction{/*** */private static final String DEFAULT_PICTURE_PATH="images/default_picture.jpg";private static final long serialVersionUID = -1163017407936568957L;private ByteArrayInputStream imageStream;private String id;private Boolean useDefault = true;private IImageHandlerService imageHandlerService;/**上传的图片文件*/private File file;/**文件名*/private String fileFileName;/*===============================================Getters and Setters Start============================================*/public ByteArrayInputStream getImageStream() {return imageStream;}public void setImageStream(ByteArrayInputStream imageStream) {this.imageStream = imageStream;}public Boolean getUseDefault() {return useDefault;}public void setUseDefault(Boolean useDefault) {this.useDefault = useDefault;}public String getId() {return id;}public void setId(String id) {this.id = id;}public IImageHandlerService getImageHandlerService() {return imageHandlerService;}public void setImageHandlerService(IImageHandlerService imageHandlerService) {this.imageHandlerService = imageHandlerService;}public File getFile() {return file;}public void setFile(File file) {this.file = file;}public String getFileFileName() {return fileFileName;}public void setFileFileName(String fileFileName) {this.fileFileName = fileFileName;}/*===============================================Getters and Setters Start============================================*//*** <p>description:根据id获得图像</p>* <p>author:朱建林</p>* <p>time:2013-8-01</p>*/public String getImageById(){boolean useDefaultImage = false;Image image = null;logger.debug("id is "+id);//判断id是否为空if(StringUtils.isEmpty(id)){useDefaultImage = true;}else{image = imageHandlerService.getImageById(id);}if(null == image || null == image.getContent()){useDefaultImage = true;}if(useDefaultImage == true && useDefault == true){String path = request.getSession().getServletContext().getRealPath("/");String imagePath = path + DEFAULT_PICTURE_PATH;logger.debug("default word imagePath : " + imagePath);try {imageStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(imagePath)));} catch (IOException e) {e.printStackTrace();}} else {imageStream = new ByteArrayInputStream(image.getContent());}return SUCCESS;}/*** 异步上传图片文件* @return* @author ShiJiapeng* @throws IOException */public String uploadByAjax() throws IOException{JSONObject json = new JSONObject();if(file == null || file.length() <= 0 || StringUtils.isEmpty(fileFileName)){json.put("success", false);json.put("message", "文件为空,请重新上传");message = json.toString();return AJAX_RETURN_TYPE;   }//获取文件后缀名String type = "";int index = fileFileName.lastIndexOf(".");if(index >= 0){type =  fileFileName.substring(index+1);}byte[] imageByte = FileUtils.readFileToByteArray(file);String id = imageHandlerService.saveImage(imageByte, type);json.put("success", true);json.put("imageId", id);json.put("imageType", type);json.put("imageName", fileFileName);message = json.toString();//防止IE下变成下载文件super.getServletResponse().setContentType("text/html");super.getServletResponse().getWriter().write(message);return null;}}

=================================================

音频上传Action

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts><package name="audiohandler" namespace="/audiohandler" extends="web-default"><!--根据id获取音频--><action name="getAudioById" class="audioHandlerAction" method="getAudioById"><result name="success" type="stream">  <param name="contentType">audio/mp3</param>  <param name="inputName">audioStream</param>  <param name="bufferSize">2048</param>  </result></action><!-- 上传音频--><action name="uploadByAjax" class="audioHandlerAction" method="uploadByAjax"><result name="ajax" type="jsonmessage"><param name="message">message</param></result></action></package></struts>

音频上传类

package com.wisemen.hhb.module.audiohandler.action;import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;import org.apache.commons.io.FileUtils;import net.sf.json.JSONObject;import com.wisemen.framework.server.action.BaseAction;
import com.wisemen.framework.server.exception.ActionException;
import com.wisemen.framework.util.StringUtils;
import com.wisemen.hhb.module.audiohandler.service.IAudioHandlerService;
import com.wisemen.hhb.module.base.domain.Audio;public class AudioHandlerAction extends BaseAction{private static final long serialVersionUID = -9087644531038401978L;private IAudioHandlerService audioHandlerService;private ByteArrayInputStream audioStream;private String id;/**上传的音频文件*/private File file;/**文件名*/private String fileFileName;/*===============================================Getters and Setters Start============================================*/public ByteArrayInputStream getAudioStream() {return audioStream;}public IAudioHandlerService getAudioHandlerService() {return audioHandlerService;}public void setAudioHandlerService(IAudioHandlerService audioHandlerService) {this.audioHandlerService = audioHandlerService;}public void setAudioStream(ByteArrayInputStream audioStream) {this.audioStream = audioStream;}public String getId() {return id;}public void setId(String id) {this.id = id;}public File getFile() {return file;}public void setFile(File file) {this.file = file;}public String getFileFileName() {return fileFileName;}public void setFileFileName(String fileFileName) {this.fileFileName = fileFileName;}/*===============================================Getters and Setters Start============================================*//*** <p>description:根据id获得音频</p>* <p>author:朱建林</p>* <p>time:2013-8-01</p>*/public String getAudioById(){Audio audio = null;boolean useDefaultImage = false;if(StringUtils.isEmpty(id)){useDefaultImage = true;logger.debug("the id of audio is null");throw new ActionException("系统错误");}else{audio = audioHandlerService.getAudioById(id);}if(null == audio || null == audio.getContent()){useDefaultImage = true;logger.debug("the audio is null");throw new ActionException("系统错误");}if(useDefaultImage == false){audioStream = new ByteArrayInputStream(audio.getContent());}return SUCCESS;}/*** 异步上传音频文件* @return* @author ShiJiapeng* @throws IOException */public String uploadByAjax() throws IOException{JSONObject json = new JSONObject();if(file == null || file.length() <= 0 || StringUtils.isEmpty(fileFileName)){json.put("success", false);json.put("message", "文件为空,请重新上传");message = json.toString();return AJAX_RETURN_TYPE;   }//获取文件后缀名String type = "";int index = fileFileName.lastIndexOf(".");if(index >= 0){type =  fileFileName.substring(index+1);}byte[] audioByte = FileUtils.readFileToByteArray(file);String audioId = audioHandlerService.saveAudio(audioByte, type);json.put("success", true);json.put("audioId", audioId);json.put("audioType", type);json.put("audioName", fileFileName);message = json.toString();logger.debug("message = " + message);//防止IE下变成下载文件super.getServletResponse().setContentType("text/html");super.getServletResponse().getWriter().write(message);return null;}}





本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/497670.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hbase获取表信息_HBase的读写和javaAPI的使用

一、hbase系统管理表hbase:namespace&#xff0c;记录了hbase中所有namespace的信息 ,当前系统下有哪些namespace信息scan hbase:namespacehbase:meta&#xff0c;记录了region信息scan hbase:meta二、读写思想client(get、scan)rowkey条件(1)由于rowkey是存储在region上的(2)判…

机器学习必知的15大框架

作者 | Devendra Desale译者 | Mags来源 | 云栖社区不管你是一个研究人员&#xff0c;还是开发者&#xff0c;亦或是管理者&#xff0c;想要使用机器学习&#xff0c;需要使用正确的工具来实现。本文介绍了当前最流行15个机器学习框架。机器学习工程师是开发产品和构建算法团队…

上传图片和音频到Server,再转存到DB

jsp页面 <% page language"java" pageEncoding"UTF-8" contentType"text/html;charsetUTF-8"%> <% include file"/common/taglibs.jsp"%> <link rel"stylesheet" href"<c:url value/plugin/ztree-v3…

区分大小屏幕_第一个Python程序——在屏幕上输出文本

本节我将给大家介绍最简单、最常用的 Python 程序——在屏幕上输出一段文本&#xff0c;包括字符串和数字。Python 使用 print 函数在屏幕上输出一段文本&#xff0c;输出结束后会自动换行。在屏幕上输出字符串字符串就是多个字符的集合&#xff0c;由双引号" "或者单…

2018 年物联网发展五大趋势预测

来源&#xff1a;腾股创投作者 &#xff1a;Pramod Chandrayan物联网已经开始在所有行业的企业中走向主流。 到 2018 年底&#xff0c;物联网支出预计将增长 15&#xff05;&#xff0c;达到 7725 亿美元&#xff0c;毫无疑问&#xff0c;未来一年连接设备和企业物联网项目数量…

js中立即执行函数会预编译吗_JavaScript预编译过程

什么是预编译&#xff1f;当js代码执行时有三个步骤&#xff1a;1.语法分析&#xff0c;这个过程检查出基本的语法错误。2&#xff0c;预编译&#xff0c;为对象分配空间。3&#xff0c;解释执行&#xff0c;解释一行执行一行&#xff0c;一旦出错立即停止执行。预编译发生在代…

知识图谱火了,但你知道它的发展历史吗?|赠书5本

作者&#xff1a;尼克 编辑&#xff5c;Emily版式由AI前线整理知识图谱火了&#xff0c;但你知道它的发展历史吗&#xff1f;本文节选自《人工智能简史》第 3 章&#xff0c;从第一个专家系统 DENDRAL 到语义网再到谷歌的开源知识图谱&#xff0c;对知识图谱的发展历程进行了…

python使用selenium_如何在python中使用selenium的示例

最近基于selenium写了一个python小工具,记录下学习记录,自己运行的环境是Ubuntu 14.04.4, Python 2.7,Chromium 49.0,ChromeDriver 2.16selenium简介selenium提供了一个通用的接口,可模拟用户来操作浏览器,比如用于自动化测试等.selenium的核心是WebDriver,它提供了一组接口,这…

英特尔爆发史诗级芯片漏洞,Linux之父痛斥英特尔公司

来源&#xff1a;CEO来信概要&#xff1a;1月4日消息&#xff0c;英特尔处理器存在芯片级漏洞的消息仍在发酵&#xff0c;恐怕将会成为计算机行业发展史上最大的安全漏洞之一。1月4日消息&#xff0c;英特尔处理器存在芯片级漏洞的消息仍在发酵&#xff0c;恐怕将会成为计算机行…

曲线均匀分布_曲线篇:深刻理解B 样条曲线(下)

前两篇中讲解了贝塞尔曲线和B样条基础。FrancisZhao&#xff1a;曲线篇: 贝塞尔曲线​zhuanlan.zhihu.comFrancisZhao&#xff1a;曲线篇&#xff1a;深刻理解B 样条曲线&#xff08;上&#xff09;​zhuanlan.zhihu.com本文讲一下B样条的进阶clamped B样条由于我们常用的B样条…

JSON.parse()解析单引号错误的问题

1、当用JSON.parse()方法&#xff0c;解析从后台传来的json字符串&#xff0c;而且字符串中含有单引号(如Iam ....)&#xff0c;页面将出现如下异常 2、页面引起报错部分 3、解决办法&#xff1a;在后台将特殊字符转义 bookBlockVoListJSONString BaseStringUtils.getJsonFor…

python复杂非线性方程_非线性方程python入门——屠龙刀

感谢师兄提供的题图。(这图应该是p的&#xff0c;我找电视剧&#xff0c;没谢三哥这三个字。)早些年做科研的时候&#xff0c;其实就用倚天屠龙走天下&#xff0c;一个矩阵对角化&#xff0c;一个非线性方程求解。当时是研究的冷原子&#xff0c;算是比较新的方向&#xff0c;量…

Facebook人工智能实验室提出「全景分割」,实现实例分割和语义分割的统一

原文来源&#xff1a;arxiv作者&#xff1a;Alexander Kirillov、Kaiming He1、Ross Girshick、Carsten Rother、Piotr Dollar「雷克世界」编译&#xff1a;嗯~阿童木呀、KABUDA现如今&#xff0c;我们提出并研究了一种新的“全景分割”&#xff08;Panoramic segmentation&…

ajax工作中使用模板

<!-- 生成课件 --><action name"createCourseware" class"coursewareManagementAction" method"createCourseware"><result name"ajax" type"jsonmessage"><param name"message">message…

数学一年级应用题_一年级数学下册应用题大全(附带答案)

应用题可以说是小学数学中最为重要的内容&#xff0c;是培养学生数学思维及解题能力的重要途径&#xff0c;做好应用题掉小学生非常重要&#xff0c;职场达人网精选100道一年级数学下册应用题&#xff0c;并附带答案。1、原有29个球&#xff0c;借出8个&#xff0c;还剩多少个&…

art.dialog弹出框后再根据其内部iframe的大小调整大小

1、主页面&#xff0c;用来弹出弹出页面 //查看错误小练习$.exerciseClick function(rowId ){$.event.get().preventDefault();var tdScore $("#" rowId).find("[aria-describedbyhomeworkStudentListGrid_score]");if( parseInt(tdScore.html()) 100 …

lg空调代码大全解决_LG空调故障代码大全

LG空调故障代码大全LG空调故障代码&#xff1b;PS/PRNA系列&#xff1b;CO&#xff1b;室内进风热敏电阻故障。C1&#xff1b;室内盘管热敏电阻故障。C4&#xff0c;室外热敏电阻故障包括室外环温&#xff0c;室外盘管&#xff0c;压机回气。C5&#xff0c;室内&#xff0c;室外…

句子录音打分代码参考

1、页面名称:sentenceReadRecordDialogAnyone.jsp 2、页面效果 3、页面源码 <% page language"java" pageEncoding"UTF-8" contentType"text/html;charsetUTF-8"%> <% include file"/common/taglibs.jsp"%><script la…

工具用途_见微知著,小工具大用途!

为什么同一个饮品&#xff0c;店家做的尝起来如此美味。拿到配方后自己做的不是甜了就是酸了呢&#xff1f;相信很多朋友都有这种疑惑。但真相往往就会在不经意间被忽视&#xff0c;很多初学者并没有老师傅那样的熟练手感&#xff0c;可以做到信手拈来的程度。当没有那些量勺&a…

带暂停功能的音频播放代码参考

1、页面来源&#xff1a;sentenceExercise07.jsp 2、页面效果 3、源代码 <% page language"java" pageEncoding"UTF-8" contentType"text/html;charsetUTF-8"%> <% include file"/common/taglibs.jsp"%> <% include f…