java实现文件上传,文件下载功能 源码
来源:互联网 发布:31岁转行做数据分析师 编辑:程序博客网 时间:2024/04/29 22:42
package test02;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
public class UploadFile {
public static void main(String[] args) {
}
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
/**
* 下载文件
* path:文件详细路径,包括文件名
*/
public void downloadFile(String path) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
// 清空response
response.reset();
// 设置response的Header
response.setBufferSize(5 * 1024 * 1024);
filename = new String(filename.getBytes("GBK"), "ISO8859_1");
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
int readLength = 0;
byte[] readUnit = new byte[1024 * 1024];
while ((readLength = fis.read(readUnit)) != -1) {
toClient.write(readUnit, 0, readLength);
toClient.flush();
}
fis.close();
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 上传文件 (如果是上传同名文件,将先删除原文件,再上传新文件)
* sPath:文件物理路径,如:c:/apacheTomcat6.0/model
* uploadFileName:文件名
* upload:File类型的要上传的文件
* 返回值:成功
*/
public String uploadFile(File upload, String uploadFileName, String sPath) {
try {
String fileName = uploadFileName;
// 获取物理路径
//String sPath = request.getRealPath(filePath);
sPath = sPath.endsWith(File.separator)?sPath:sPath+File.separator;
String outputFileName = sPath + fileName;
File outputPathFile = new File(sPath);
if (!outputPathFile.exists()) {
outputPathFile.mkdirs();
}
//保存文件
File outputFile = new File(outputFileName);
InputStream is = new FileInputStream(upload);
OutputStream os = new FileOutputStream(outputFile);
if (outputFile.exists())
outputFile.delete();
byte buffer[] = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
return "false";
}
return "success";
}
}
//Jar包下载链接 http://download.csdn.net/download/da_zhuang/5196573
- java实现文件上传,文件下载功能 源码
- JAVA实现FTP服务器文件的上传,下载,删除功能
- Java实现文件上传功能
- ASP .net 实现文件上传 下载功能
- Struts2-上传/下载文件功能实现
- 文件上传与下载的功能实现
- struts2实现文件上传与下载功能
- Java实现文件下载功能
- Java实现文件下载功能
- Java文件下载功能实现
- java实现文件下载功能
- java 下载文件功能实现
- SmartUpload上传组件实现文件的上传和下载功能
- 文件上传功能实现
- JAVA实现邮箱上传文件功能
- java web文件上传功能实现
- java实现文件上传ftp服务器功能
- Java实现文件上传和下载
- 指针加减操作,当a为数组时a和&a的区别
- 从下拉菜单设计细节看Amazon对用户体验的把握,商品分类下拉菜单
- 选择外汇交易品种很重要
- TPMC
- javascript去除字符串两端的空格
- java实现文件上传,文件下载功能 源码
- php抓取页面方法汇总
- DEVCAPS1
- 外网远程桌面连接局域终端
- 002 Figuring in C/C++
- Ubuntu 下安装配置 JDK
- 位操作练习
- Oracle 合并连续时间段
- 老罗是个锤子