java 文件下载的多种形式
1.复制本地文件
/**
* 复制本地文件
* @param sourcePath
* @param newPath
*/
public static void copyFile(String sourcePath, String newPath){
File fileSource = new File(sourcePath);
File fileNew = new File(newPath);
String fileName = sourcePath.substring(sourcePath.lastIndexOf("\\") + 1);
//测试新目录中有无同名文件
File[] files = fileNew.listFiles();
for (File file : files) {
if(file.getName().equals(fileName))
fileName += "_1";
}
String newFileName = newPath + File.separator + fileName;
File targetFile = new File(newFileName);
FileInputStream in = null;
FileOutputStream out = null;
BufferedInputStream bin = null;
BufferedOutputStream bout = null;
try {
in = new FileInputStream(fileSource);
bin = new BufferedInputStream(in);
out = new FileOutputStream(targetFile);
bout = new BufferedOutputStream(out);
byte buf[] = new byte[1024];
int length = bin.read(buf);
//保存文件
while (length != -1){
bout.write(buf, 0, length);
length = bin.read(buf);
bout.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try{
if (bin != null){
bin.close();
}
}catch (Exception e){
System.out.println("关闭输入流错误!");
}
try{
if (bout != null){
bout.close();
}
}catch (Exception e){
System.out.println("关闭输出流错误!");
}
}
}
2.下载网络文件到本地服务器
/**
*下载网络文件到本地服务器
* @param request
* @param response
* @param filePath
* @throws MalformedURLException
*/
public void downLoadContractPdf(HttpServletRequest request, HttpServletResponse response, String filePath,String reuqetUrl) throws MalformedURLException {
URL url = new URL(reuqetUrl);
response.setContentType("text/html;charset=utf-8");
String name = "123.pdf";
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
try {
URLConnection conn = url.openConnection();
InputStream fileStream = conn.getInputStream();
//获取下载文件所在路径
String path = request.getServletContext().getRealPath(filePath + name);
//文件
File file = new File(path);
//判断文件是否存在
if (file.exists()) {
//控制下载文件的名字
response.addHeader("Content-Disposition", "attachment;filename=" + name);
byte buf[] = new byte[1024];
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
os = response.getOutputStream();
int i = bis.read(buf);
while (i != -1) {
os.write(buf, 0, i);
i = bis.read(buf);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.下载网络文件(这里我给设置成了pdf格式文件)到浏览器所在的pc端
/**
* 下载网络文件(设定为pdf格式)到浏览器所在的pc
* @throws MalformedURLException
*/
public static void downLoadContractPdf(String requestUrl, HttpServletResponse response) throws MalformedURLException {
String name = (System.currentTimeMillis()) + ".pdf";
URL url = new URL(requestUrl);
File file = new File(name);
InputStream fileStream = null;
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment;filename=" + name);
try {
URLConnection conn = url.openConnection();
fileStream = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(fileStream);
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte buf[] = new byte[1024];
int length = bis.read(buf);
//保存文件
while (length != -1){
bos.write(buf, 0, length);
length = bis.read(buf);
bos.flush();
}
bos.close();
bis.close();
} catch (Exception e) {
System.out.println("文件下载失败,失败原因:" + e);
}
}
第三种若需要设置其他样式可根据自己的业务需求修改
文件格式类型列表
这里需要注意,着重分清业务需求,下载网络文件到本地服务器和下载网络文件到览器的pc不能混为一谈
以上代码亲测有效