系统的要求之一是能够上传用户的个人资料图片。它在本地主机中运行良好。我在网上上传的图片正是本地成功上传的图片。这种错误的可能原因是什么?
这是简化的代码(我之前做的代码比较复杂,但是没有代码重复,运行速度也比较快。我简化代码是为了追查问题,但运气不好)
@RequestMapping(value = "/patient_dp.it")
public String uploadPatientDP(HttpSession session, HttpServletRequest request, Model model, @ModelAttribute("form") ArchiveForm form, @RequestParam("id") long id, @RequestParam("key") String key) {
System.err.println("patient dp");
String username = session.getAttribute("user").toString();
session.setAttribute("user", username);
Patient patient = null;
try {
patient = patientDao.getPatientById(id);
MultipartFile file = form.getImages().get(0);
String absolutePath = null;
String relativePath = null;
InputStream inputStream = null;
OutputStream outputStream = null;
String fileName_Original = null;
if (file.getSize() > 0) {
inputStream = file.getInputStream();
if (file.getSize() > 100000) {
System.out.println("File Size:::" + file.getSize());
return "redirect:/upload_prof_pic_assembler?id=" + id + "&key=" + key;
}
fileName_Original = file.getOriginalFilename();
System.out.println("size::" + file.getSize());
relativePath = "images/profilepic/" + fileName_Original;
absolutePath = request.getSession().getServletContext().getRealPath("") + "/" + relativePath;
outputStream = new FileOutputStream(absolutePath);
System.out.println("fileName:" + file.getOriginalFilename());
int readBytes = 0;
byte[] buffer = new byte[100000];
while ((readBytes = inputStream.read(buffer, 0, 100000)) != -1) {
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
}
patient.setRelativePath(relativePath);
patient.setFileName(fileName_Original);
patientDao.update(patient);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "redirect:/view_patient_profile.it?id=" + patient.getId();
}
在上传图片jsp中,效果很好
<c:when test="${patient != null }">
<form:form action="patient_dp.it?id=${patient.id }&key=p" method="post" commandName="form" enctype="multipart/form-data">
Select File: <input type="file" name="images[0]" id="image" onSubmit="return Validate();"/>
<a href=""><span class="btn"><input type="submit" value="Upload" class="btnNew"></span></a>
</form:form>
</c:when>
这是 view-patient-profile.jsp 文件的摘录。由于图像的路径不会保留在 Jelastic (mariadb) 上的在线数据库中,它会转向默认图像。
<c:choose>
<c:when test="${patient.getRelativePath() == null || patient.getRelativePath().equalsIgnoreCase('null')}">
<img src="resources/images/img_user.jpg" alt="">
</c:when>
<c:otherwise>
<img src="${patient.getRelativePath() }" alt="${patient.getPersonInfo().getFirstName()}'s profile picture" />
</c:otherwise>
</c:choose>