图片存放目录F:\work\hanz\WebRoot\upload
图片表:photolist
id imagurl
1 upload/XXXX.JPG1
2 upload/XXXX.JPG2
我要做的是后台上传图片!我前台的图片显示是通过读取photolist表中imagurl中图片路径来显示图片的!
希望做过网站后台图片上传的前辈给与指点!
图片表:photolist
id imagurl
1 upload/XXXX.JPG1
2 upload/XXXX.JPG2
我要做的是后台上传图片!我前台的图片显示是通过读取photolist表中imagurl中图片路径来显示图片的!
希望做过网站后台图片上传的前辈给与指点!
解决方案 »
- 什么叫类被包包装了
- 页面传值不通过,请求帮助!
- java中如何得到所在工程的名字?
- JSP+tomcat下使用javabean出现异常。求助
- jsp中实现时间提醒
- Tomcat下Oracle驱动加载失败问题
- 欢迎加入java高级群:84459794 不怕你来,就怕你错过!
- 初学者的Servlet问题,求大家帮帮我,顶也送分,分不够再加。
- 如何提交下拉框控件的值到jsp页面,我用的是连接传递参数的方式,而不是表单提交,大家快帮忙。
- springboot整合mybatise出现mybatis-spring-boot-starter-test不兼容的问题
- 求一正则表达式-靠右对齐的数字
- 急,jsp问题
FileOutputStream fos = FileOutputStream("F:\work\hanz\WebRoot\upload"+myFile.getFileName());
fos.write...
fos.flush..
然后在这 往数据库里 插一条 数据
id imagurl
myFile.getFileName() "upload"+myFile.getFileName()
fos.close();不知道行不行。你用的是 struts 吗?
<form action="upload.do" method="post" enctype="multipart/form-data">
文件名: <input type="text" name="filename"/><br>
选择文件:<input type="file" name="file" value="select"/><br>
<input type="submit" value="submit"/>
</form />2、建ActionForm 名为 uploadActionForm
private String filename;
private FormFile file;
getter and setter
3、建Action
UpLoadActionForm ulf = (UpLoadActionForm)form;
FormFile file = ulf.getFile();
FileOutputStream fos = new FileOutputStream("F:\work\hanz\WebRoot\upload"+ulf.getFilename());
fos.write(file.getFileData());
fos.flush();
//在这里往数据库里插 相应数据
fos.close();
2. 文件名,那就是简单的isnert操作。读取则相反啊。你是哪步不会?
file文本框得到的也是路径
在insert方法里面时
if(this.isTokenValid(request)){ //struts令牌
this.saveImg(request, imgFile, fileForm); //保存图片
fileService.insertFile(fileForm.getFile());//插入数据
this.resetToken(request);//struts令牌
}//保存图片
private void saveImg(HttpServletRequest request,FormFile imgFile,FileForm fileForm){
if (imgFile != null && imgFile.getFileSize() > 0) {
String fileName = imgFile.getFileName();
String sqlPath = "img/" + fileName;
//图片所在路径
String savePath = request.getSession().getServletContext().getRealPath("/")+ "img\\" + fileName;
System.out.println(fileName);
System.out.println(sqlPath);
System.out.println(savePath);
HttpSession session=request.getSession();
session.setAttribute("savePath", savePath);
session.setMaxInactiveInterval(60*60);
//String savePath1=(String)session.getAttribute("savePath");
// 数据库
fileForm.getFile().setFileEmpPhoto(sqlPath);
// 文件
try {
InputStream input = imgFile.getInputStream();
FileOutputStream output = new FileOutputStream(savePath);
byte[] b = new byte[1024];
while (input.read(b) != -1) {
output.write(b);
b = new byte[1024];
}
output.close();
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public ActionForward insertFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
FileForm fileForm=(FileForm)form;
System.out.println(fileForm.getFile().getPositionName().getPositionNameId());
FormFile imgFile=fileForm.getImgFile();
try {
if(this.isTokenValid(request)){
this.saveImg(request, imgFile, fileForm);
fileService.insertFile(fileForm.getFile());
this.resetToken(request);
}
response.setContentType("text/html;charset=GBK");
response.setCharacterEncoding("GBK");
PrintWriter writer = response.getWriter();
writer.write(" <script>alert('档案信息登记成功!');window.location.href='/HR_four/file.do?action=findAllFile'; </script>");
writer.close();
return null;
// return this.findAllFile(mapping, form, request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return mapping.findForward("error");
}
}-----------------------//保存图片
private void saveImg(HttpServletRequest request,FormFile imgFile,FileForm fileForm){
if (imgFile != null && imgFile.getFileSize() > 0) {
String fileName = imgFile.getFileName();
String sqlPath = "img/" + fileName;
//图片所在路径
String savePath = request.getSession().getServletContext().getRealPath("/")+ "img\\" + fileName;
System.out.println(fileName);
System.out.println(sqlPath);
System.out.println(savePath);
HttpSession session=request.getSession();
session.setAttribute("savePath", savePath);
session.setMaxInactiveInterval(60*60);
//String savePath1=(String)session.getAttribute("savePath");
// 数据库
fileForm.getFile().setFileEmpPhoto(sqlPath);
// 文件
try {
InputStream input = imgFile.getInputStream();
FileOutputStream output = new FileOutputStream(savePath);
byte[] b = new byte[1024];
while (input.read(b) != -1) {
output.write(b);
b = new byte[1024];
}
output.close();
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
比如photolist
id imageaurl
1 uploadfile/xxxx.jpg