上传下载后端

import java.io.File;
import java.util.List;
import java.util.UUID;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;


import com.bw.bean.Xiaokai;
import com.bw.service.FserviceI;
import com.bw.utils.FileDownLoad;


@Controller
public class Fcontroller {


@Autowired
private FserviceI fservice;

@RequestMapping("list")
public String getflist(HttpServletRequest request) {
List<Xiaokai> flist=fservice.getflist();
request.setAttribute("flist",flist);
return "flist";
}

@RequestMapping("flist")
public List<Xiaokai> getlist() {
List<Xiaokai> flist=fservice.getflist();
return flist;
}

@RequestMapping("upfile.do")
public String up(MultipartFile file,HttpServletRequest request,Xiaokai xiaokai){
//获得文件浏览器里名字
String originalFilename = file.getOriginalFilename();
//获取随机前缀名
String prefix = UUID.randomUUID().toString().replace("-", "");
//真实名字
String newFileName=prefix+originalFilename;
//获取文件夹真实路径
String realPath = request.getSession().getServletContext().getRealPath("")+"\\photo\\";

File file2 = new File(realPath,newFileName);

xiaokai.setServername(newFileName);
xiaokai.setRealname(originalFilename);
//添加到数据库
fservice.add(xiaokai);
try {
file.transferTo(file2);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return "redirect:list.do";
}

//下载
@RequestMapping("download")
public void download(String servername,HttpServletRequest request,HttpServletResponse response) throws Exception {
System.out.println(servername);
String path="/photo/"+servername;
FileDownLoad.download(path,request,response);
}

}

下边是

工具类:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class FileDownLoad {


public static String download(String filepath,HttpServletRequest request, HttpServletResponse response) throws Exception{

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;


try {
// 锟斤拷锟斤拷谴臃锟斤拷锟斤拷锟斤拷锟饺★拷锟斤拷锟斤拷锟斤拷锟斤拷锟较低筹拷木锟斤拷路锟斤拷锟斤拷锟斤拷锟斤拷
//String filepath = request.getRealPath(filepatha);//锟斤拷锟斤拷锟斤拷时锟斤拷
String filepathall = request.getSession().getServletContext().getRealPath(filepath);


File uploadFile = new File(filepathall);


//图片锟斤拷锟斤拷锟斤拷
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);

//锟矫碉拷锟侥硷拷锟斤拷
String filename = filepath.substring(filepath.lastIndexOf("\\")+1);


// 锟斤拷锟斤拷途锟斤拷堑锟斤拷锟斤拷锟斤拷囟曰锟斤拷锟侥关硷拷锟斤拷锟�
response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(filename, "utf-8"));

int bytesRead = 0;
// 锟斤拷锟斤拷锟斤拷锟饺バ达拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.flush();
}
if (fis != null) {
fis.close();
}
if (bis != null) {
bis.close();
}
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

}

//前端代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="./css/css.css">
<script type="text/javascript" src="./js/jquery-1.8.3.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    function addfile(fid){
     location.href="add.jsp";
    }
    
    function download(servername){
     location.href="download.do?servername="+servername;
    }
</script>
</head>
<body><input type="button" name="file"  value="上传" οnclick="addfile()">
<table>
<tr>
<td>编号</td>
<td>姓名</td>
<td>真实名字</td>
<td>服务器名字</td>
<td>操作</td>

</tr>
<c:forEach items="${flist }" var="f">
<tr>
<td>${f.fid }</td>
<td>${f.fname }</td>
<td>${f.realname }</td>
<td>${f.servername }<img src="photo/${f.servername}" width="40px" height="40px"></td>
<td>

<input type="button" name="down"  value="下载" οnclick="download('${f.servername}')"></td>
</tr>
</c:forEach>
</table>


</body>

</html>

//add

<%@ 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>
<link rel="stylesheet" type="text/css" href="./css/css.css">
<script type="text/javascript" src="./js/jquery-1.8.3.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">


</script>
</head>
<body>
<form action="upfile.do" enctype="multipart/form-data" method="post">
文件编号:<input type="text" name="fid"><br>
姓名:<input type="text" name="fname"><br>
<input type="file" name="file"><br>
<input type="submit"  value="上传"><br>
</form>
</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值