多平台统一管理软件接口,如何实现多平台统一管理软件接口
205
2023-07-03
java组件commons
最近做了一个文件上传、下载、与在线打开文件的功能,刚开始对文件上传的界面中含有其它表单(例如输入框、密码等)在上传的过程中遇到了许多问题,下面我写了一个同时实现文件上传、下载、在线打开文件的测试程序。
首先请看效果图:
核心代码:
package com.jefry;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL; CyBrC
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class FileUpload
*/
public class FileUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String fileDir = "F:/";
/**
* Default constructor.
*/
public FileUpload() {
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String aFileName = request.getParameter("fileName");
String online = request.getParameter("online");
FileInputStream in = null;
ServletOutputStream out = null;
boolean isOnLine = online != null ? true : false ;
try {
if(isOnLine){
URL u = new URL("file:///"+fileDir + aFileName);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename="+aFileName);
}
else{
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + aFileName);
}
in = new FileInputStream(fileDir + aFileName);
out = response.getOutputStream();
out.flush();
int aRead = 0;
while ((aRead = in.read()) != -1 & in != null) {
out.write(aRead);
}
out.flush();
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
DiskFileItemFactory fileFactory = new DiskFileItemFactory();
ServletFileUpload fu = new ServletFileUpload(fileFactory);
List fileItems = fu.parseRequest(request);
Iterator iter = fileItems.iterator();
String uploader = null;
String date = null;
List
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) { // 文件
String oldFileName = item.getName();
String newFileName = null;
int delimiter = oldFileName.lastIndexOf("/");
if (delimiter == -1)
newFileName = oldFileName.substring(delimiter + 1);
else
newFileName = oldFileName;
fileNames.add(newFileName);
item.write(new File(fileDir + newFileName));
} else { // 表单
String fieldName = item.getFieldName();
if ("uploader".equals(fieldName)) CyBrC{
uploader = item.getString();
} else if ("date".equals(fieldName)) {
date = item.getString();
}
}
}
request.setAttribute("fileNames",fileNames);
request.getRequestDispatcher("download.jsp").forward(request, response);
} catch (Exception e) {
}
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
文件一:
文件二:
上传者:
日期:
download.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
--------------直接下载------------
<%
List
for(String fileName : fileNames) {
%>
<%
}
%>
--------------直接打开---------
<%
for(String fileName : fileNames) {
%>
<%
}
%>
本文已被整理到了《Java上传操作技巧汇总》,欢迎大家学习阅读。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~