struts2上传图片到oracle数据库中
请问怎么样用struts2上传图片到oracle数据库中?有原代码演示吗? --------------------编程问答-------------------- 上传图片一般都保存的是图片路径,但要是相对路径,。。 --------------------编程问答-------------------- blob. --------------------编程问答-------------------- 保存路径比较好一点,数据库字段设成blob也行。下面的源码是在fileLoader.jsp上传文件到某目录下,跳转到fileGet.jsp显示图片并提供下载
怎么存数据库,楼主自己看着办吧。。。
---上传文件JSP:fileLoader.jsp---
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts2 File Upload</title>
</head>
<body>
<form action="fileLoader!save" method="post" enctype="multipart/form-data">
<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td>个人照片</td>
</tr>
<tr>
<td>
<s:file label="图片" name="photoFile"></s:file>
</td>
</tr>
<tr>
<td><s:submit value="确定" /></td>
</tr>
</table>
</form>
</body>
---显示并下载jsp:fileGet.jsp---
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts2 File DownLoad</title>
</head>
<body>
<form action="fileLoader!download" method="post"
enctype="multipart/form-data">
<table width="100%" border="1" align="center" cellpadding="0"
cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td>上传文件为:</td>
<td><img
src="<s:property value="'/Struts2Upload/UploadImages/' + photoFileFileName"/>" />
<input type="hidden" id="img" name="img"
value="<s:property value="photoFileFileName"/>"></td>
</tr>
<tr>
<td><s:submit value="下载" /></td>
</tr>
</table>
</form>
</body>
</html>
</html> --------------------编程问答-------------------- ---Action类:FileLoaderAction---
--------------------编程问答-------------------- struts.xml自己配吧,我正好有样例,直接拷上来了。。。。 --------------------编程问答-------------------- 谢谢大家哦!!我试试!! --------------------编程问答-------------------- 这个好使 试了好几天终于找到能实现的了
/**
* 图片的上传和下载
*
* @author Join
*/
public class FileLoaderAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16 * 1024;
// 页面图片
private File photoFile;
// 图片名
private String photoFileFileName;
// 图片类型
private String photoFileContentType;
private String img;
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public File getPhotoFile() {
return photoFile;
}
public void setPhotoFile(File photoFile) {
this.photoFile = photoFile;
}
public String getPhotoFileFileName() {
return photoFileFileName;
}
public void setPhotoFileFileName(String photoFileFileName) {
this.photoFileFileName = photoFileFileName;
}
public String getPhotoFileContentType() {
return photoFileContentType;
}
public void setPhotoFileContentType(String photoFileContentType) {
this.photoFileContentType = photoFileContentType;
}
/*
* 取得运行时服务器目录
*
* @return 服务器根目录
*/
public String getSavePath() {
// 如果是从服务器上取就用这个获得系统的绝对路径方法。
return ServletActionContext.getServletContext().getRealPath("/");
}
public String download() throws UnsupportedEncodingException {
String path = getSavePath() + "\\UploadImages\\" + this.getImg(); System.out.println("path is " + path);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
String filepath = path;
System.out.println("文件路径" + filepath);
File uploadFile = new File(filepath);
try {
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = ServletActionContext.getResponse().getOutputStream();
bos = new BufferedOutputStream(fos);
// 这个就就是弹出下载对话框的关键代码
ServletActionContext.getResponse().setHeader("Content-disposition",
"attachment;filename=" + URLEncoder.encode(path, "utf-8"));
int bytesRead = 0;
// 这个地方的同上传的一样。我就不多说了,都是用输入流进行先读,然后用输出流去写,唯一不同的是我用的是缓冲输入输出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String save() throws Exception {
if (null != this.getPhotoFile()) {
// 从画面上取得图片文件
File files = this.getPhotoFile();
// 文件名 = 文件名 + 日期
photoFileFileName = new Date().getTime() + photoFileFileName.trim();
String savePath = getSavePath() + "\\UploadImages\\";
// 判断保存用文件夹是否存在
mkdir(savePath);
// 保存用的数据流生成
FileOutputStream fos = new FileOutputStream(savePath + photoFileFileName);
System.out.println(savePath + photoFileFileName);
// 保存文件
FileInputStream fis = new FileInputStream(files);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
return "Loaded";
}
/*
* 判断保存用文件夹是否存在
*
* @param path 保存用的文件夹路径
*/
public void mkdir(String path) {
File file = new File(path);
// 如果存在
if (file.exists()) {
System.out.println("the dir is exits");
// 如果不存在,新建
} else {
file.mkdir();
System.out.println("have made a dir");
}
}
}
补充:Java , Java相关