求struts和hibernate实现图片上传保存到数据库中,并可以在页面显示出来,并显示上传人的名字的代码啊?

2025-02-27 02:55:20
推荐回答(2个)
回答1:

struts.xml中











image/bmp,image/png,image/gif,image/jpeg,image/pjpeg




add2.jsp
showUpload1.jsp


applicationContext.xml中

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

class="org.springframework.jdbc.datasource.DriverManagerDataSource">
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">






class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">






org.hibernate.dialect.SQLServerDialect

true




entity/Dome.hbm.xml
















read-only="true" />
read-only="true" />
read-only="true" />
read-only="true" />














expression="execution(* biz.impl.*.*(..))" />



UploadOneAction.java类中
package web;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import biz.DomeBiz;

import com.opensymphony.xwork2.ActionSupport;

import entity.Dome;

public class UploadOneAction extends ActionSupport {
private static final long serialVersionUID = 572146812454l;
private static final int BUFFER_SIZE = 16 * 1024;
private File myFile; //文件流
private String contentType; //内容类型
private String fileName; //文件名
private String imageFileName; //新文件名
private String caption; //标题

DomeBiz domeBiz ;
public void setDomeBiz(DomeBiz domeBiz) {
this.domeBiz = domeBiz;
}
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}

public String execute() {
if (myFile == null)
return INPUT;
System.out.println(this.getMyFileFileName()+"123");
imageFileName=new Date().getTime()+ getExtention(this.getMyFileFileName());
//得到图片保存的位置(根据root来得到图片保存的路径在tomcat下的该工程里)
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("images")+ "/" + imageFileName);
copy(myFile, imageFile); //把图片写入到上面设置的路径里
Dome dome = new Dome();
dome.setPicture(this.getImageFileName());
domeBiz.save(dome);
return SUCCESS;
}
//得到原文件名称
public String getMyFileFileName() {
return fileName;
}

public void setMyFileFileName(String fileName) {
this.fileName = fileName;
}

public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
}
add2.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




上传


enctype="multipart/form-data">







showUpload1.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>






显示图片



style="padding: 3px; border: solid 1px #cccccc; text-align: center">
' />






回答2:

你单独的要这个东西,就算大家都会也不可能帮你先写一个或者帮你一点点去找代码,你就去csdn下一个现成的java编写 什么管理系统之类的,比如图书管理系统. 直接看人家写的源码,

相关问答