struts文件上传

 

BookFile实体

public class BookFile implements Serializable{
   
    
    private String file_id;
    private String real_name;
    private String content_type;
    private Timestamp createdate;
    public String getFile_id() {
        return file_id;
    }
    public void setFile_id(String file_id) {
        this.file_id = file_id;
    }
    public String getReal_name() {
        return real_name;
    }
    public void setReal_name(String real_name) {
        this.real_name = real_name;
    }
    public String getContent_type() {
        return content_type;
    }
    public void setContent_type(String content_type) {
        this.content_type = content_type;
    }
    public Timestamp getCreatedate() {
        return createdate;
    }
    public void setCreatedate(Timestamp createdate) {
        this.createdate = createdate;
    }
    @Override
    public String toString() {
        return "BookFile [file_id=" + file_id + ", real_name=" + real_name + ", content_type=" + content_type
                + ", createdate=" + createdate + "]";
    }

 FileDao

public class FileDao extends BaseDao{

    
    //1.新增一条书本图片信息
    //2.根据书本id修改书本信息对应的图片id
    public void addBookFile(Book book,BookFile bookFile) {
        //创建一个文本id
        String fileId=UUID.randomUUID().toString().replace("-", "");
        String sql1="insert into t_book_file(file_id,real_name,content_type)"
                + "values ('"+fileId+"','"+bookFile.getReal_name()+"','"+bookFile.getContent_type()+"')";
        
        String sql2="update t_mvc_book set image='"+fileId+"' where bid="+book.getBid();
       super.executeUpdateBatach(new String[]{
           sql1,
           sql2,
       });
       
    }
    
    //根据文件id获取对应的书本文件信息(file_id real_name content_type)
    public BookFile querySingleBookFile(String fileId) {
        String sql="select * from t_book_file where file_id='"+fileId+"'";
    return super.executeEntity(sql, BookFile.class);
    }
}

 FileAction

 

public class FileAction extends BaseAction {
   private FileDao fileDao=new FileDao();
    
   private final String PATH="/uploads";
  
    //书本编号
    private Integer bid;
    
    //文件id
    private String fileId;
    //书本图片
    private File image;
    //书本图片名称
    private String imageFileName;
    
    //书本图片类型
    private String imageContentType;

    

    public String getFileId() {
        return fileId;
    }

    public void setFileId(String fileId) {
        this.fileId = fileId;
    }

    public Integer getBid() {
        return bid;
    }

    public void setBid(Integer bid) {
        this.bid = bid;
    }

    public File getImage() {
        return image;
    }

    public void setImage(File image) {
        this.image = image;
    }

    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }

    public String getImageContentType() {
        return imageContentType;
    }

    public void setImageContentType(String imageContentType) {
        this.imageContentType = imageContentType;
    }
    
    
    
    
    //文件上传
    public String upload() throws Exception {
        //1.新增一条书本图片信息
        //2.根据书本id修改书本信息对应的图片id
        Book book=new Book();
        book.setBid(bid);
        BookFile bookFile=new BookFile();
        bookFile.setContent_type(imageContentType);
        bookFile.setReal_name(imageFileName);
        
        System.out.println(bid);
        System.out.println(imageContentType);
        System.out.println(imageFileName);
        
        fileDao.addBookFile(book, bookFile);
        //3.上传文件
        //拼接文件上传路径
        String filePath=PATH+File.separator+imageFileName;
        System.out.println(filePath);
        //将相对路径转化成绝对路径
        String absolutePath=this.transforPath(filePath);
        
        //定义输入流
        InputStream is=new FileInputStream(image);
        //定义输出流
        OutputStream out=new FileOutputStream(new File(absolutePath));
        //定义每次读取多少字节
        byte[] by=new byte[1024];
        //每次读取的长度
        int len=0;
        //当长度等等于-1,跳出循环
        while(-1!=(len=is.read(by))) {
            out.write(by);
        }
        //关闭输入输出流
        is.close();
        out.close();
        return "list";
    }
    
    @SuppressWarnings("unused")
    private String transforPath(String filepath) {
        return request.getRealPath(filepath);
    }
    
    //文件下载
    public String dowload() throws Exception {
        //1.根据书本文件id获取对应的书本文件信息
        BookFile bookFile=fileDao.querySingleBookFile(fileId);
        //2.下载
        //拼接文件下载路径
        String filepath=PATH+File.separator+bookFile.getReal_name();
        //将相对路径转化成绝对路径
        String absolutePath=this.transforPath(filepath);
        //1. 内容类型
           response.setContentType(bookFile.getContent_type());

         //3. 处理文件名的中文乱码
           String fileName = bookFile.getReal_name();
           fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
            
           
        //2. 设置响应头
           response.setHeader("Content-Disposition","attachment;filename=" + fileName);//文件名
        
        //定义输入流
           InputStream is=new  FileInputStream(new File(absolutePath));
        //定义输出流
           OutputStream out=response.getOutputStream();
         //定义每次读取多少字节
            byte[] by=new byte[1024];
            //每次读取的长度
            int len=0;
            //当长度等等于-1,跳出循环
            while(-1!=(len=is.read(by))) {
                out.write(by);
            }
            //关闭输入输出流
            is.close();
            out.close();
            return null;
    }

 bookFile.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>文件上传</h1>

<form action="fileAction_upload.action" enctype="multipart/form-data" method="post">
<label>书本编号</label>
<input type="text" readonly="readonly" name="bid" value="${param.id}"><br>
<label>书本图片</label>
<input type="file" name="image"><br>
<input type="submit" value="文件上传">
</form>
</body>
</html>

  bookList

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="z" uri="/MyJSP"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>书本管理,当前时间戳<%=System.currentTimeMillis() %></h1>

<form action="bookAction_queryBook.action" method="post">
<label>书本名称</label>
<input type="text" name="bname">
<input type="submit" value="查询">
</form>
<a href="bookAdd.jsp">新增</a>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<th>书本编号</th>
<th>书本名称</th>
<th>书本价格</th>
<th>书本图片</th>
<th>操作</th>

<tbody>
<c:forEach items="${result.queryBook}" var="book">
  <tr>
  <td>${book.bid}</td>
  <td>${book.bname}</td>
  <td>${book.price}</td>
  <td>
  <c:if test="${empty book.image }">
     未上传图片
  </c:if>
  <c:if test="${ not empty book.image }">
   <img width="auth" height="100px" src="fileAction_dowload.action?fileId=${book.image}"/>
  </c:if>
  
  </td>
  <td>
  <a href="bookAction_delBook.action?bid=${book.bid}">删除</a>
  <a href="bookAction_book.action?type=edit&bid=${book.bid}">修改</a>
   <a href="bookAction_book.action? type=detail&bid=${book.bid}">详情</a>
   <a href="bookFile.jsp?id=${book.bid}">文件上传</a>
   <a href="fileAction_dowload.action?fileId=${book.image}">文件下载</a>
  
  </td>
  </tr>
</c:forEach>
</tbody>
</table>
     <%--  <z:page bean="${result.pageBean}"></z:page> --%>  
</body>
</html>

 

 

posted @ 2019-07-30 21:39  小蜜疯  阅读(122)  评论(0编辑  收藏  举报