gpt4 book ai didi

java - 当我使用 servlet 将 Android 图像保存到数据库时,为什么它会损坏

转载 作者:行者123 更新时间:2023-11-29 22:34:39 24 4
gpt4 key购买 nike

所以我在 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

我真的不知道这段代码出了什么问题。其他图像(从网站上传)效果很好。谁能帮我吗?

最佳答案

我解决了我的问题。 Base64 解码效果不太好。

我导入了 sun.misc.BASE64Decoder 并将其转换如下:

String imageString64 = request.getParameter("image");
BASE64Decoder decoder = new BASE64Decoder();
byte[] image = decoder.decodeBuffer(imageString64);

关于java - 当我使用 servlet 将 Android 图像保存到数据库时,为什么它会损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29590192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com