gpt4 book ai didi

java - struts 2.3上传前预览图片

转载 作者:可可西里 更新时间:2023-11-01 08:25:40 24 4
gpt4 key购买 nike

我想使用 struts2 在数据库中上传文件,但问题是我想在同一页面上选择照片时显示照片。

图像将显示程序的这些。

enter image description here

索引.jsp

  <s:form action="UploadAction.action" enctype="multipart/form-data">
<s:textfield name="ename" required="true" label="Emp Name" />
<s:file name="photo" label="Photo" required="true" />
<s:submit value="U P L O A D" />

</s:form>

UAction.java

  public class UAction {

String ename, photo, msg;

public String execute() throws Exception {
msg = com.db.Admin.addemp(ename, photo);
setEname("");
return "SUCCESS";
}

照片.java

  public class Photo extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String empno = request.getParameter("e");
try {
ResultSet rs = com.db.Admin.getPhoto(empno);
if (rs.next()) {
Blob ph = (Blob) rs.getBlob(1);
byte data[] = ph.getBytes(1, (int) ph.length());
ServletOutputStream out = response.getOutputStream();
out.write(data);
}
} catch (Exception e) {
e.getMessage();
}
}

管理.java

  public static String addemp(String ename, String photo) {
try {
CallableStatement cs = connect().prepareCall("{call addEmp(?,?,?)}");

cs.setString(1, ename);
FileInputStream r = new FileInputStream(photo);
cs.setBinaryStream(2, r);
cs.registerOutParameter(3, Types.VARCHAR);
cs.execute();
return cs.getString(3);
} catch (Exception e) {
return e.getMessage();
}
}

public static ResultSet getData() throws Exception {
return connect().prepareCall("{call getData()}").executeQuery();
}

public static ResultSet getPhoto(String empno) throws Exception {
CallableStatement cs = connect().prepareCall("{call getPhoto(?)}");
cs.setString(1, empno);
return cs.executeQuery();
}

最佳答案

这应该用一些 javascript 代码来完成。

<img id="imagePlaceHolder" alt="your image" width="100" height="100" />
<s:file name="photo" label="Photo" required="true"
onchange="document.getElementById('imagePlaceHolder').src = window.URL.createObjectURL(this.files[0])"
/>

您可以在 Preview an image before it is uploaded 找到更多解决方案

关于java - struts 2.3上传前预览图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36288826/

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