所以我在Android上的ImageView中有一个图像。我将位图转换为PNG文件并将其编码为Base64字符串。然后我将此字符串作为参数发布在POST请求的主体中,以及其他一些细节。
private void uploadImage() {
img.buildDrawingCache();
Bitmap bmp = img.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 90, stream);
byteArray = Base64.encodeToString(stream.toByteArray(), Base64.DEFAULT);
StringRequest req = new StringRequest(Request.Method.POST, url+"/AddFoto", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError response) {
}
}){
@Override
protected Map<String, String> getParams(){
Map <String, String> params = new HashMap<String, String>();
Log.d(ProfileFragment.class.getSimpleName(), params.toString());
params.put("tussenstopID",tussenstopID.toString());
params.put("description", description);
params.put("image", byteArray);
Log.d("image byte array", byteArray);
return params;
}
};
AppController.getInstance().addToRequestQueue(req);
}
然后我的servlet收到这个文件并将其转换为字节数组,以便我可以将它上传到数据库。
package Servlets;
import Core.Foto;
import Core.Tussenstop;
import DAO.FotoFacade;
import DAO.TussenstopFacade;
import com.sun.xml.messaging.saaj.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Lukas
*/
public class AddFoto extends HttpServlet {
@EJB
FotoFacade fotoFacade;
@EJB
TussenstopFacade tussenstopFacade;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
Long tussenstopID = Long.parseLong(request.getParameter("tussenstopID"));
String description = request.getParameter("description");
String imageString64 = request.getParameter("image");
System.out.println(imageString64);
String imageString = Base64.base64Decode(imageString64);
byte[] image = imageString.getBytes();
upload(image, description, tussenstopID);
}
}
public void upload(byte[] imageString, String descr, Long tussenstopID) {
try {
InputStream fin2 = new ByteArrayInputStream(imageString);
//InputStream fin2 = new ByteArrayInputStream( imageString.getBytes() );
Class.forName("com.mysql.jdbc.Driver");
// Connect to the database
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/Project_1?user=root&password=root");
// Set autocommit to false to manage it by hand
connection.setAutoCommit(false);
PreparedStatement pre = connection.prepareStatement("INSERT INTO foto (beschrijving,image,tussenstop_id) values(?,?,?)");
pre.setString(1, descr);
pre.setBinaryStream(2, fin2);
Long tempID=tussenstopID;
pre.setLong(3, tempID);
pre.executeUpdate();
System.out.println("Inserting Successfully!");
connection.commit();
pre.close();
} catch (Exception e) {
System.out.println("Exception-File Upload." + e.getMessage());
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
一切顺利,图像与我发送的其他参数一起添加到数据库中。但是,图像不可读。当我打开图像的“文本”时,我可以清楚地看到它在开始时显示PNG(实际上前面有1个字符)。字节看起来也不错:http://i.stack.imgur.com/L1vaJ.jpg
我真的不知道这段代码有什么问题。其他图片(从网站上传)工作得很好。任何人都可以帮助我吗?
答案 0 :(得分:0)
我解决了我的问题。 base64解码效果不佳。
我导入了sun.misc.BASE64Decoder并将其转换为:
String imageString64 = request.getParameter("image");
BASE64Decoder decoder = new BASE64Decoder();
byte[] image = decoder.decodeBuffer(imageString64);