文件上传与下载,跪求,在线等。
如题,文件的上传与下载。a.jsp页面可以上传,上传的文件要显示在jsp页面,然后a.jsp页面的内容被提交到b.jsp页面以后,文件可以允许下载,就和邮箱的附件完全一样。我现在已经可以上传了,但是不知道怎么显示页面,而且也不知道该把什么东西存入数据库中。我需要jsp页面和后台的完整代码,io读取的,急求,谢谢。 --------------------编程问答-------------------- 怎么没人?泪奔~ --------------------编程问答--------------------/**--------------------编程问答-------------------- 楼上都给了,这么好啊,我来拿分…… --------------------编程问答--------------------
* 上传文件, 返回上传成功的文件个数, 可以读取文件上传的实时进度
* @param folder 文件存放的目录,物理路径
* @param newFileName 保存为新文件的名称(支持多个文件同时上传)
* @return int
*/
@SuppressWarnings("unchecked")
public int uploadFile(String folder, String []newFileName) {
int num = -1;
try {
File folderFile = new File(folder);
FileUtil.getBean().createFolder(folder);
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(MAX_FILE_SIZE);
upload.setProgressListener(new UploadListener(getRequest().getSession()));
getRequest().setCharacterEncoding("UTF-8");
Iterator iterator = upload.parseRequest(getRequest()).iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();//源文件
if (!item.isFormField() && !"".equals(item.getName())) {
num++;
if (num < newFileName.length) {
OutputStream output = new FileOutputStream(new File(folderFile, newFileName[num]));
InputStream input = item.getInputStream();
try {
byte[] buffer = new byte[1024];
int i = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);//保存文件
}
} catch (Exception e) {
e.printStackTrace();
} finally {
input.close();
output.close();
}
} else {
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return num + 1;
}
/**
* 文件上传时读取即时的上传进度(返回json格式字符串)
* @return String
*/
public String readUploadProgress() {
String result = "";
UploadInfo info = (UploadInfo) getRequest().getSession().getAttribute(Settled.UPLOAD_INFO);
if (null != info) {
return "{readSize:\"" + info.getReadSize() +
"\",fileSize:\"" + info.getFileSize() +
"\",num:\"" + info.getNum() +
"\",percent:\"" + info.getPercent() + "\"}";
}
return result;
}
/**
* 下载文件
* @param filePath 文件路径(物理路径)
* @param fileName 源文件名称
*/
public void downLoadFile(String filePath, String fileName) {
File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
return;
}
InputStream input =null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = getResponse().getOutputStream();
getResponse().setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
byte[] buffer = new byte[1024];
int i = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(null !=output) {
output.flush();
output.close();
}
if(null !=input) {
input.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
getResponse().getOutputStream();
这个报错,原因是没有这个方法。什么情况? --------------------编程问答-------------------- 不好意思,这几个忘记粘贴了
/**--------------------编程问答-------------------- /**
* @see 获得request
* @return HttpServletRequest
* */
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
/**
* @see 获得response
* @return HttpServletResponse
* */
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
/**
* @see 获得session
* @return HttpSession
* */
public HttpSession getSession() {
return getRequest().getSession();
}
/**
* @see 获得application
* @return ServletContext
* */
public ServletContext getApplication() {
return getSession().getServletContext();
}
/**
* @see 获得项目的真实路径
* @return String
* */
public String getBase() {
return getApplication().getRealPath("/");
}
* 上传文件, 返回上传成功的文件个数, 可以读取文件上传的实时进度
* @param folder 文件存放的目录,物理路径
* @param newFileName 保存为新文件的名称(支持多个文件同时上传)
* @return int
*/
@SuppressWarnings("unchecked")
public int uploadFile(String folder, String []newFileName) {
int num = -1;
try {
File folderFile = new File(folder);
FileUtil.getBean().createFolder(folder);
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(MAX_FILE_SIZE);
upload.setProgressListener(new UploadListener(getRequest().getSession()));
getRequest().setCharacterEncoding("UTF-8");
Iterator iterator = upload.parseRequest(getRequest()).iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();//源文件
if (!item.isFormField() && !"".equals(item.getName())) {
num++;
if (num < newFileName.length) {
OutputStream output = new FileOutputStream(new File(folderFile, newFileName[num]));
InputStream input = item.getInputStream();
try {
byte[] buffer = new byte[1024];
int i = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);//保存文件
}
} catch (Exception e) {
e.printStackTrace();
} finally {
input.close();
output.close();
}
} else {
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return num + 1;
}
/**
* 文件上传时读取即时的上传进度(返回json格式字符串)
* @return String
*/
public String readUploadProgress() {
String result = "";
UploadInfo info = (UploadInfo) getRequest().getSession().getAttribute(Settled.UPLOAD_INFO);
if (null != info) {
return "{readSize:\"" + info.getReadSize() +
"\",fileSize:\"" + info.getFileSize() +
"\",num:\"" + info.getNum() +
"\",percent:\"" + info.getPercent() + "\"}";
}
return result;
}
/**
* 下载文件
* @param filePath 文件路径(物理路径)
* @param fileName 源文件名称
*/
public void downLoadFile(String filePath, String fileName) {
File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
return;
}
InputStream input =null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = getResponse().getOutputStream();
getResponse().setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
byte[] buffer = new byte[1024];
int i = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(null !=output) {
output.flush();
output.close();
}
if(null !=input) {
input.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
补充:Java , Web 开发