struts+hibernate 怎么将上传文件的名字,路径,大小存入mysql数据库,最好有详细代码的,
展开全部
①使用struts2的上传jar包,commons-fileupload.jar
②配置struts配置文件,在action下配置上传文件的拦截器,例
<action name="uploadAction" class="com.action.UploadAction ">
<result name="success">
index.jsp
</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes"></param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
</action>
③用action处理上传文件(下面有例子UploadAction.java)
④将路径和大小使用Hibernate存入数据库。
下面是上传的action类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
public class UploadAction extends BaseAction {
public UploadAction() {
super("");
}
private static final long serialVersionUID = -7091926458432816740L;
private static final int BUFFER_SIZE = 16 * 1024;
// 使用File数组封装多个文件域对应的文件内容
private File[] file;
// 使用字符串数组封装多个文件域对应的文件名
private String[] fileFileName;
// 接受依赖注入的属性
@SuppressWarnings("unused")
private String savePath;
private String path;
// 结果显示消息
private String msg;
// 只用这个方法也可以实现多图片上传功能
public String execute() throws Exception {
System.out.println(ServletActionContext.getRequest().getSession()
.getServletContext().getRealPath("/"));
// 取得需要上传的文件数组
File[] files = this.getFile();
// 遍历每个需要上传的文件
for (int i = 0; i < files.length; i++) {
fileFileName[i] = i + new Date().getTime()
+ getExtention(fileFileName)[i];
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath()
+ "upload/img\\" + getFileFileName()[i]);
System.out.println(getSavePath() + "upload\\img\\"
+ getFileFileName()[i]);
path = "/upload/img/" + getFileFileName()[i];
// 以每个需要上传的文件建立文件输入流
FileInputStream fis = new FileInputStream(files[i]);
// 将每个需要上传的文件写到服务器对应的文件中
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
//HttpServletRequest request = ServletActionContext.getRequest();
//String paths = request.getContextPath();
//path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+paths+"/";
this.msg = "上传成功!";
return SUCCESS;
}
// 获得每个文件的扩展名
public String[] getExtention(String[] fileNames) {
String[] extentions = new String[fileNames.length];
for (int i = 0; i < extentions.length; i++) {
int pos = fileNames[i].lastIndexOf(".");
extentions[i] = fileNames[i].substring(pos);
}
return extentions;
}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}
public String getSavePath() {
return ServletActionContext.getRequest().getSession()
.getServletContext().getRealPath("/");
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
②配置struts配置文件,在action下配置上传文件的拦截器,例
<action name="uploadAction" class="com.action.UploadAction ">
<result name="success">
index.jsp
</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes"></param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
</action>
③用action处理上传文件(下面有例子UploadAction.java)
④将路径和大小使用Hibernate存入数据库。
下面是上传的action类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
public class UploadAction extends BaseAction {
public UploadAction() {
super("");
}
private static final long serialVersionUID = -7091926458432816740L;
private static final int BUFFER_SIZE = 16 * 1024;
// 使用File数组封装多个文件域对应的文件内容
private File[] file;
// 使用字符串数组封装多个文件域对应的文件名
private String[] fileFileName;
// 接受依赖注入的属性
@SuppressWarnings("unused")
private String savePath;
private String path;
// 结果显示消息
private String msg;
// 只用这个方法也可以实现多图片上传功能
public String execute() throws Exception {
System.out.println(ServletActionContext.getRequest().getSession()
.getServletContext().getRealPath("/"));
// 取得需要上传的文件数组
File[] files = this.getFile();
// 遍历每个需要上传的文件
for (int i = 0; i < files.length; i++) {
fileFileName[i] = i + new Date().getTime()
+ getExtention(fileFileName)[i];
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath()
+ "upload/img\\" + getFileFileName()[i]);
System.out.println(getSavePath() + "upload\\img\\"
+ getFileFileName()[i]);
path = "/upload/img/" + getFileFileName()[i];
// 以每个需要上传的文件建立文件输入流
FileInputStream fis = new FileInputStream(files[i]);
// 将每个需要上传的文件写到服务器对应的文件中
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
//HttpServletRequest request = ServletActionContext.getRequest();
//String paths = request.getContextPath();
//path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+paths+"/";
this.msg = "上传成功!";
return SUCCESS;
}
// 获得每个文件的扩展名
public String[] getExtention(String[] fileNames) {
String[] extentions = new String[fileNames.length];
for (int i = 0; i < extentions.length; i++) {
int pos = fileNames[i].lastIndexOf(".");
extentions[i] = fileNames[i].substring(pos);
}
return extentions;
}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}
public String getSavePath() {
return ServletActionContext.getRequest().getSession()
.getServletContext().getRealPath("/");
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
参考资料: 有什么问题,可以追问或者去我空间留言!~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询