|
发表于 2015-8-21 17:07:38
|
显示全部楼层
Failed to load resource: the server responded with a status of 405 (Method Not Allowed) http://localhost:4400/baas/uploadImageAjax
后台的servlet怎么写的呢?上传时就提示上面的信息![](static/image/smiley/default/mad.gif)
我写的代码是
package com.justep.baas.uploadImageAjax;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import javax.naming.NamingException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import sun.misc.BASE64Decoder;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.data.Util;
public class UploadImageAjaxServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 4942153269752607096L;
/**
*
*/
//存储前台传过来的图片
public void uploadImageAjax(ServletRequest request, ServletResponse response) throws ParseException, SQLException, NamingException, IOException {
// 参数序列化
JSONObject params = (JSONObject) JSONObject.parse(request.getParameter("params"));
JSONObject jsonObj = new JSONObject();
String image = params.getString("image"); //拿到字符串格式的图片
String subPath=params.getString("subPath");
String PicName=params.getString(" icName");
System.out.println(PicName);
// 只允许jpg
String header ="data:image/jpeg;base64,";
// 去掉头部
image=image.split(",")[1];
image = image.substring(header.length());
// 写入磁盘
String success = "fail";
BASE64Decoder decoder = new BASE64Decoder();
try{
byte[] decodedBytes = decoder.decodeBuffer(image); //将字符串格式的image转为二进制流(biye[])的decodedBytes
String imgFilePath =subPath+PicName; //指定图片要存放的位置
FileOutputStream out = new FileOutputStream(imgFilePath); //新建一个文件输出器,并为它指定输出位置imgFilePath
out.write(decodedBytes); //利用文件输出器将二进制格式decodedBytes输出
out.close(); //关闭文件输出器
success = "上传文件成功!";
System.out.println("上传文件成功!");
}catch(Exception e){
success = "上传文件失败!|"+e.getMessage();
e.printStackTrace();
}
finally{
jsonObj.put("success", success);
Util.writeJsonToResponse(response, jsonObj);
}
}
}
有什么不对的吗? |
|