struts.xml中
image/bmp,image/png,image/gif,image/jpeg,image/pjpeg
applicationContext.xml中
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">
org.hibernate.dialect.SQLServerDialect
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+"/";
%>
你单独的要这个东西,就算大家都会也不可能帮你先写一个或者帮你一点点去找代码,你就去csdn下一个现成的java编写 什么管理系统之类的,比如图书管理系统. 直接看人家写的源码,