今天 写文件更新的时候 ,因为将原图片放在了新输入图片的后面,后台先按照非文件读取信息,然后因为逻辑问题,一直导致yuanImg为空
<!-- hidden 放在bigImg 上面 后台for 循环读取 按顺序 yuanImg 为空 -->
<input type="hidden" name = "yuanImg" value = "<%=b.getBigImg() %>">
<label class="layui-form-label">图书照片:</label>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Book book = new Book();
String yuanImg = "";
//当使用文件上传的时候 切记不能使用request.getParameter() 取数据
String path = request.getServletContext().getRealPath("/upload");
//1创建文件磁盘工厂
PrintWriter out = response.getWriter();
DiskFileItemFactory df = new DiskFileItemFactory();
df.setRepository(new File(path));
//2创建文件上传类
ServletFileUpload sf = new ServletFileUpload(df);
sf.setHeaderEncoding("utf-8");
sf.setFileSizeMax(1024*1024);
//3转换request,将所有request数据得到(普通数据和图片在一起)
try {
List<FileItem> fileItems = sf.parseRequest(request);
for (FileItem fileItem : fileItems) {
if(fileItem.isFormField()) { //普通字段
if(fileItem.getFieldName().equals("bookName")) {
book.setBookName(fileItem.getString("utf-8"));
}if(fileItem.getFieldName().equals("bookAuthor")) {
book.setAuthor(fileItem.getString("utf-8"));
}if(fileItem.getFieldName().equals("context")) {
book.setContext(fileItem.getString("utf-8"));
}if(fileItem.getFieldName().equals("yuanImg")) {
yuanImg = fileItem.getString("utf-8");
System.out.println(yuanImg);
}
if(fileItem.getFieldName().equals("id")) {
book.setBookId(Integer.parseInt((fileItem.getString("utf-8"))));
}if(fileItem.getFieldName().equals("bookTypeId")) {
book.setBookTypeId(Integer.parseInt((fileItem.getString("utf-8"))));
}if(fileItem.getFieldName().equals("bookPrice")) {
book.setPrice(Float.parseFloat((fileItem.getString("utf-8"))));
}if(fileItem.getFieldName().equals("hyPrice")) {
book.setHyPrice(Float.parseFloat((fileItem.getString("utf-8"))));
}if(fileItem.getFieldName().equals("bookcount")) {
book.setNum(Integer.parseInt((fileItem.getString("utf-8"))));
}
if(fileItem.getFieldName().equals("PbDate")) {
book.setPbDate(sdf.parse(fileItem.getString("utf-8")));
}if(fileItem.getFieldName().equals("sjDate")) {
book.setSjDate(sdf.parse(fileItem.getString("utf-8")));
}
}else{
String fname = fileItem.getName();//获取文件名称
if(fname!=null && !fname.equals("")){
fname = System.currentTimeMillis()+fname.substring(fname.lastIndexOf("."));
book.setBigImg(fname);
File f = new File(path+"/"+fname);
fileItem.write(f);
File delFile = new File(path+"/"+yuanImg);
if(delFile.exists()){
delFile.delete();
}
}else {
System.out.println(yuanImg + "----------------");
book.setBigImg(yuanImg);
System.out.println(book.getBigImg() + "+++++++++++++++++++++++++++");
}
}
}
if(new BookDao().update(book) ==1) {
out.print("<script>alert('更新成功');location='listSer'</script>)");
out.flush();
out.close();
return;
}else {
out.print("<script>alert('更新失败');history.go(-1)</script>)");
out.flush();
out.close();
return;
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
有时候代码顺序可以导致一个隐藏的问题,前后端交互一定要注意,如果是两个人写的代码,问题会很严重