87,907
社区成员
发帖
与我相关
我的任务
分享
OutputStream outstr = response.getOutputStream();
BufferedOutputStream bufferOut = new BufferedOutputStream(outstr);
InputStream inStr = dao.readFile(savePath + imagePath, 0);
BufferedInputStream bufferIn = new BufferedInputStream(inStr);
byte[] buffer = new byte[4096];
int size = bufferIn.read(buffer);
while(size != -1){
bufferOut.write(buffer, 0, size);
size = bufferIn.read(buffer);
}
System.out.println("read files pass");
bufferIn.close();
bufferOut.flush();
bufferOut.close();
function showImages(){
var filePath = $("#imagePath").val();
$.ajax({type: "post", //使用post方法访问后台
dataType: "text", //返回text格式的数据,(这里存在疑问,Java部分是response输出流,这里应该指定什么样的数据类型)
url: http+"select.pic", //要访问的后台地址,这里是servlet的地址
data:{"filePath":filePath}, //要发送的数据
success: function(data){//data为返回的数据,在这里做数据绑定
$("#images").attr("src", "data:image/gif;base64," + data);
alert("complete!");
});
}
<?php
if($_POST) {
echo base64_encode(file_get_contents('../'.$_POST['img'])); //读取图片数据并作base64编码
exit;
}
?>
<img id='images' src=''>
<script src=/scripts/jquery-1.8.3.min.js></script>
<script>
$(function() {
$.post('<?=$_SERVER['PHP_SELF']?>', {img:'03.gif'}, function(data) {
$("#images").attr("src", "data:image/gif;base64," + data);
});
});
</script>
虽然语言不同,但是原理是一样的