求大神教我怎么用java的servlet无任何组件的情况下上传或下载文件上传下载
解决方案 »
- 怎么把excel的路径传到后台
- 为什么struts2.2.1总是试图调用action里的public String index()方法
- 关于HQL查询 请高手来,深夜急问
- 用过Pager-taglib的朋友请进!
- frame 问题,在线
- 一个简单的问题,请大家帮帮忙!!(分已用尽,请原凉)
- 开发过知识管理的,提供些思路,相关资料,或列程,或相关网站
- 我自己建的servlet不能运行,请问在tomcat中应该则么配置?(给高分)急!!!
- 动态菜单的问题,急!
- myelipse http://localhost:8080/javaPrj_1/index.jsp 打不开
- request.getParameter复选框一直为null
- 用html:file上传文件时,文件太大无响应
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// step1,创建一个DiskFileItemFactory对象
// 为解析器提供解析时的缺省的配置。
DiskFileItemFactory dfif = new DiskFileItemFactory();
// step2,创建一个解析器
ServletFileUpload sfu = new ServletFileUpload(dfif);
// step3,使用解析器解析
try {
// FileItem对象封装了一个表单域当中的所有的
// 数据。 List<FileItem> items = sfu.parseRequest(request);
for (int i = 0; i < items.size(); i++) {
FileItem item = items.get(i);
// 要判断是一个普通的表单域还是
// 上传文件域
if (item.isFormField()) { // 是一个普通的表单域
String name = item.getFieldName();
String value = item.getString();
System.out.println(name + ":" + value);
} else {
// 上传文件域,要将文件保存在服务器端
ServletContext sc = this.getServletContext(); // 获得实际部署时物理路径
String path = sc.getRealPath("upload");
System.out.println(path);
// 获得上传文件的名称
String fileName = item.getName();
System.out.println(fileName);
File file = new File(path + "//" + fileName);
System.out.println(file);
item.write(file);
}
} } catch (Exception e) {
e.printStackTrace(); }
}
引入jar包:
貌似这个不能用啊,DiskFileItemFactory和ServletFileUpload这两个类报错
貌似这个不能用啊,DiskFileItemFactory和ServletFileUpload这两个类报错引jar包呀
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import javax.servlet.http.HttpServletResponse;public class FileUtil2 {
/**
* 上传文件
*
* @param file
* @param filePath
*
* @throws Exception
*/
public static void runUploadFile(File file, String filePath) throws Exception
{
InputStream streamIn = null;
OutputStream streamOut = null;
if (file != null)
{
try
{
streamOut = new FileOutputStream(filePath);
streamIn = new FileInputStream(file);; int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) { streamOut.write(buffer, 0, bytesRead);}
}catch(IOException e)
{
e.fillInStackTrace();
}finally
{
try
{
if(streamIn != null)
streamIn.close();
if(streamOut != null)
streamOut.close();
}
catch (IOException e)
{
}
}
}
}
/**
* 下载文件
*
* @param filePath
* @param response
*
* @throws Exception
*/
public static void runDownFile(String filePath, HttpServletResponse response) throws Exception
{
try
{
File file = new File(filePath);
String fileName = new String(filePath.substring(filePath.lastIndexOf("\\")+1, filePath.length()).getBytes("gbk"),"ISO-8859-1"); //windows
response.setContentType("application");
//response.setHeader("Content-Disposition", "attachment; filename=" + fileName.replace(fileName.subSequence(fileName.lastIndexOf("."), fileName.length()), ".zip"));
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
FileInputStream fis=new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] b=new byte[1024];//相当于我们的缓存
long k=0;//该值用于计算当前实际下载了多少字节
//从response对象中得到输出流,准备下载
OutputStream myout=response.getOutputStream();
//开始循环下载
while(k<file.length()){
int j=buff.read(b,0,1024);
k+=j;
//将b中的数据写到客户端的内存
myout.write(b,0,j);
}
//将写入到客户端的内存的数据,刷新到磁盘
buff.close();
fis.close();
myout.close();
myout.flush(); }
catch (FileNotFoundException se)
{
se.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}