在做文件上传的时候,其实上传早就做完了,就是再想如何上传把路径保存到数据库,在网上找了好久,都要疯了,其实就是一个值,my,以后一定要弄清楚币表单要传的是什么。
思路:把文件的路径存放到WebRoot下的某个特定的文件夹,将信息保存到数据库,要用的时候在查找
1、获得文件名的后缀
String aa = myFileName.substring(myFileName.lastIndexOf("."));
2./获得上传图片的服务器路径
输出为:
D:\soft\eclipse_Mars\eclipse_Mars.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\GD\images
String path = ServletActionContext.getServletContext().getRealPath("/images");
3.文件上传的后台代码
将文件路径保存到数据库
package net.xx.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import net.xx.domain.FilePath;
import net.xx.service.IUploadService;
@Controller
@Scope("prototype")
public class UploadAction extends ActionSupport{
@Autowired
private IUploadService uploadService;
private File my;
private String myContentType;
private String myFileName;
private FilePath filepath;
public File getMy() {
return my;
}
public void setMy(File my) {
this.my = my;
}
public String getMyContentType() {
return myContentType;
}
public void setMyContentType(String myContentType) {
this.myContentType = myContentType;
}
public String getMyFileName() {
return myFileName;
}
public void setMyFileName(String myFileName) {
this.myFileName = myFileName;
}
public FilePath getFilepath() {
return filepath;
}
public void setFilepath(FilePath filepath) {
this.filepath = filepath;
}
public String execute() throws IOException {
System.out.println(my);
System.out.println(myContentType);
System.out.println(myFileName);
InputStream is = new FileInputStream(my);
//获得上传图片的服务器路径
String path = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(path);
// uploadService.add(ppath);
OutputStream os = new FileOutputStream(path + myFileName);
String aa = myFileName.substring(myFileName.lastIndexOf("."));
System.out.println(aa);
byte[] buff = new byte[1024*1024];
int length = 0;
while ((length = is.read(buff, 0, buff.length)) != -1) {
os.write(buff);
}
os.close();
is.close();
return SUCCESS;
}
public String add(){
uploadService.add(filepath);
return Action.SUCCESS;
}
}
1)dao接口和实现类
这个主要是将路径保存到数据库的dao
package net.xx.dao;
import net.xx.domain.FilePath;
public interface IUpLoadDao {
public void saveLujing(FilePath filepath);
}
dao的实现
package net.xx.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import net.xx.domain.FilePath;
@Repository
public class UploadDaoImpl implements IUpLoadDao{
@Autowired
private SessionFactory sessionfactory;
@Override
public void saveLujing(FilePath filepath) {
// TODO Auto-generated method stub
sessionfactory.getCurrentSession().save(filepath);
}
}
2)service的接口和实现
package net.xx.service;
import java.util.List;
import net.xx.domain.Usert;
public interface IUserService {
public void saveUser(Usert user);
public boolean findUser(Usert user);
List list();
public List getUserByUsername(String username);
List checkUser(String username);
}
实现
package net.xx.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.xx.dao.IUpLoadDao;
import net.xx.domain.FilePath;
@Service
public class UploadServiceImpl implements IUploadService{
@Autowired
private IUpLoadDao uploadDao;
@Override
public void add(FilePath filepath) {
// TODO Auto-generated method stub
uploadDao.saveLujing(filepath);
}
}
4.配置文件
1)struts.xml
<action name="UploadAction_*" class="net.xx.controller.UploadAction" method="{1}">
<!-- <result name="success">success.jsp</result> -->
<!-- <result name="input">index1.jsp</result> -->
<!-- <interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">1048576</param>
<param name="fileUpload.allowedTypes">application/vnd.ms-excel,image/jpeg</param>
</interceptor-ref> -->
<result name="success" type="json">
<param name="root"></param>
</result>
</action>
2)hibernate表的配置
只设了一个主键,自动增长
5.前台代码用的form
只是不想用jsp,所以就用了html
在form表单里有enctype=”multipart/form-data”
在正常的项目中数据库的是自己建的,而不是利用映射生成的,我在这个例子中,只是写了主键和其他列的属性,在之前做的例子中,没有在配置文件中设置主键和外键的关系,而是在数据库利用建表的语句,创建的表,并且也是利用sql语句进行主键,以及主键和外外键的设置。自我感觉好像不在配置文件中写表和表之间的关系,运行起来也没有什么问题。不知道自己想的对不对。
<class name="net.xx.domain.FilePath">
<id name="id">
<generator class="native"></generator>
</id>
<property name="path"></property>
</class>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="js/jquery-easyui-1.5/jquery.min.js"></script>
</head>
<body>
<form action="http://localhost:8080/GD/UploadAction_execute.action" method="post" enctype="multipart/form-data">
// 必须有,其中的my很重要,是我们上传的那个图片的位置,即图片原来在C盘,就是C盘
<input type="file" name="my" id="file"><br>
<input type="submit" value="上传" id="shangchuan">
</form>
<script>
$("#shangchuan").click(function () {
//获取my的值,进行添加,放到参数里
var a = document.getElementById('file').value;
alert(a);
$.ajax({
type:"post",
async:false,
url:"http://localhost:8080/GD/UploadAction_add.action",
data:{
"filepath.path" : a
},
dataType:"json",
success:function () {
alert("添加成功");
}
})
})
</script>
</body>
</html>