在ssm项目中实现图片上传并回显
在前端页面通过选择内存中图片,提交后将图片存在对应的路径中,路径存进数据库,查询数据库返回的路径通过在jsp中用img src=“”来展示图片。
实体层:
package com.po;
import org.springframework.web.multipart.MultipartFile;
public class Choice {
int qid;
String year;
String type;
String topic;
String image;
String opt;
String answer;
String remark;
private MultipartFile logoImage;
public Choice() {
super();
}
public Choice(int qid, String year, String type, String topic, String image, String opt, String answer, String remark, MultipartFile logoImage) {
this.qid = qid;
this.year = year;
this.type = type;
this.topic = topic;
this.image = image;
this.opt = opt;
this.answer = answer;
this.remark = remark;
this.logoImage = logoImage;
}
public int getQid() {
return qid;
}
public void setQid(int qid) {
this.qid = qid;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getOpt() {
return opt;
}
public void setOpt(String opt) {
this.opt = opt;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public MultipartFile getLogoImage() {
return logoImage;
}
public void setLogoImage(MultipartFile logoImage) {
this.logoImage = logoImage;
}
@Override
public String toString() {
return "Choice{" +
"qid=" + qid +
", year='" + year + '\'' +
", type='" + type + '\'' +
", topic='" + topic + '\'' +
", image='" + image + '\'' +
", opt='" + opt + '\'' +
", answer='" + answer + '\'' +
", remark='" + remark + '\'' +
", logoImage=" + logoImage +
'}';
}
}
Dao层
void insertChoice(Choice choice);
xml
<insert id="insertChoice" parameterType="Choice">
insert into Choice(year,type,topic,image,opt,answer,remark)
values(#{year},#{type},#{topic},#{image},#{opt},#{answer},#{remark});
</insert>
service层接口
public String addChoice(Choice choice,HttpServletRequest request);
@Override
public String addChoice(Choice choice, HttpServletRequest request) {
String newFileName = "";
String fileName = choice.getLogoImage().getOriginalFilename();
//选择了文件
if(fileName.length() > 0){
String realpath = request.getServletContext().getRealPath("/image");
//实现文件上传
String fileType = fileName.substring(fileName.lastIndexOf('.'));
//防止文件名重名
newFileName = MyUtil.getStringID() + fileType;
choice.setImage("/image/"+newFileName);
File targetFile = new File(realpath, newFileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//上传
try {
choice.getLogoImage().transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
}
//保存到数据库中
cMapper.insertChoice(choice);
return "choice/choicelist";
}
控制层
@RequestMapping("/choicepreinsert")
public String chiocePreinsert(){
return "choice/choiceadd";
}
在实现过程也是出现了不少问题,所以要注意细节