文件上传:
1.上传到数据库(oa)
2.上传到文件服务器(一般内存较大)
3.上传到普通服务器(这里使用tomcat)
- 上传:
例子(普通服务器 io包):
首先在你的子控制器中声明这三个变量提供get/set(注意file这个变量名指的是你从jsp页面跳转的name,名字对应即可,后面的类型和名字一样)
- 代码
public String upLoad() {
try {
StudentDAO studentDAO = new StudentDAO();
student.setPathname(fileFileName);
student.setType(fileContentType);
studentDAO.editFile(student);
FileUtils.copyFile(file,new File(application.getRealPath("/image/")+fileFileName));
if(fileFileName!=null&&!"".equals(fileFileName)) {
return SUCCESS;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return FAIL;
}
上传核心:
FileUtils.copyFile(file,new File(application.getRealPath("/image/")+fileFileName));
这里的file就是我们声明的file文件,后面是页面所在服务器的路径
图片上传好之后就在这里了
2 . 下载
jsp:
java:
public void downLoad() {
String name=request.getParameter("pathname");
if(name!=null&&!"".equals(name)) {
response.setContentType(request.getParameter("type"));
response.setHeader("Content-Disposition","attachment;filename=" +name);//�ļ���
try {
FileUtils.copyFile(new File(application.getRealPath("/image/")+name), response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 步骤:
-
内容类型 response.setContentType(d.getMime());
-
-设置响应头 response.setHeader(“Content-Disposition”,“attachment;filename=” +
fileName);//文件名 -
FileUtils.copyFile(new
File(application.getRealPath("/image/")+name),
response.getOutputStream());下载(列如google会将图片下载到你浏览器指定的目录)