图片上传方法
来源:互联网 发布:java第一阶段测试题 编辑:程序博客网 时间:2024/05/08 10:59
@ResponseBody
public String uploadImage(String imgdata, HttpServletRequest request){
BASE64Decoder decoder = new BASE64Decoder();
FileOutputStream outputStream =null;
// String uploadSavePath = "D:\\spread-bcn\\upload\\image\\";
String uploadSavePath = PropertiesConfig.getImagePath();
String returnString = "";
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat df=new SimpleDateFormat("yyyyMMddhhmmss");
String today=sdf.format(new Date());
//String savePath = PropertiesConfig.getImagePath()+today;
//检查扩展名
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + ".png";
String savePath = uploadSavePath+today;
File dirFile = new File(savePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
savePath = TxtUtil.converSeparator(savePath);
// new一个文件对象用来保存图片
File imageFile = new File(savePath,newFileName);
// 创建输出流
outputStream = new FileOutputStream(imageFile);
// 获得一个图片文件流
byte[] result = decoder.decodeBuffer(imgdata.split(",")[1]);//解码
for (int i = 0; i < result.length; ++i) {
if (result[i] < 0) {// 调整异常数据
result[i] += 256;
}
}
outputStream.write(result);
returnString= uploadSavePath+today+File.separator+newFileName;
// returnString= today+File.separator+newFileName;
} catch (IOException e) {
return "error";
} finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return returnString;
}
function onChangeUploadImage(that){
var filepath=$(that).val();
if(filepath=="")
{
return;
}
var extStart=filepath.lastIndexOf(".");
var ext=filepath.substring(extStart,filepath.length).toUpperCase();
if(".jpg|.png|.jpeg".toUpperCase().indexOf(ext.toUpperCase())==-1){
alert("只允许上传jpg、png、jpeg格式的图片");
return false;
}
//以图片宽度为800进行压缩
lrz(that.files[0], {
width: 800,
quality: 0.8
}).then(function (rst) {
$.ajax({
type: "POST",
url : "${ctx}"+"/countrySubInfo/uploadImage",
data : {
imgdata:rst.base64//压缩后的base值
},
dataType:"text",
success : function(data) {
if ("error"==data) {
alert("上传文件失败!");
}else{
alert(data);
$("#filepathsave").val(data);
//uploadFile(data);
}
},
error : function(){
alert("上传失败");
}
});
});
}
<input type="file" class="mp_file" name="file" id="files"
onchange="onChangeUploadImage(this)" multiple="multiple"
style="opacity: 1"> <img src="${ctx}/${subject.picUrl}"
id="imgs">
<%-- <input name="imageUrl" id="imageUrl" type="hidden" value="${subject.picUrl}" /> --%>
<input name="filepathsave" id="filepathsave" type="hidden" value="${subject.picUrl}"/>
<!--<span>请上传图片</span>-->
- 图片上传的方法!
- 上传图片的方法
- 上传图片预览方法
- 上传GIF图片方法!
- tinymce上传图片方法
- 图片上传方法
- 上传图片的方法
- 原生方法上传图片
- Webview图片上传方法
- 调试上传图片方法
- 原生上传图片方法
- 图片上传方法
- springmvc上传图片方法
- springmvc 文件上传,图片上传的方法
- 上传图片的简单方法
- Asp.net 图片上传方法
- IOS上传图片的方法
- IOS上传图片的方法
- 大数据之数据仓库
- webuploader上传按钮不能点击
- php导出excel表格
- linux发送文件到其他linux
- centos7安装
- 图片上传方法
- Android Fiddler抓包
- 1102:平方和与立方和
- 蓝牙的音频通路
- PAT 1044. Shopping in Mars (25)
- 本小小群主诚邀一个妹子加入本群(群很小28人)
- 虚拟机检测技术攻防
- 物化视图调优
- 跟着吴恩达学深度学习:用Scala实现神经网络-第二课:用Scala实现多层神经网络