用Java实现 上传
来源:互联网 发布:阿里云管理终端密码 编辑:程序博客网 时间:2024/05/21 11:27
说是上传,但我感觉跟平时用的ctrl+c ctrl+v感觉很像,也许是放在本地上使用的原因吧。
我是用servlet+jsp 实现le功能,简单明了,方便以后复习
UploadServlet.java
package servlet;
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.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
String path = null;
byte[] b = null;
int length = 0;
try {
path = (String) req.getParameter("file");//通过form提交,取得上传文件的路径
File file = new File(path);//新建一个路径为path的file
String serverPath="c://test//"+file.getName(); //‘c://test//‘为服务器上将要存放文件的路径 file.getName()为上传文件的文件名 这里是原名存储
inputStream = new FileInputStream(file);//定义一个file的数据流,这个数据流是用来读取file的内容的
File file2 = new File(serverPath); //在服务器上新建一个路径为‘c://test//‘ 名为 file.getName()的文件
outputStream = new FileOutputStream(file2);//定义一个数据流,这个数据流使用来往file2中写东西的
b = new byte[1024];//定义一个数组,用来临时存储数据的,【1024】是一次读取1024个字节的内容
while ((length=inputStream.read(b)) > 0) {//inputStream.read(b) 将每次读取的1024个字节的内容存放到b里 这里会返回一个int变量,这个变量是反映当前实际存储到b里的数据长度
outputStream.write(b,0,length);//将数据b用outputStream写入
}
outputStream.flush();//数据流写完后一定要刷新,否则文件中的内容不完整
inputStream.close();//关闭读取流
outputStream.close();//关闭写入流
} catch (FileNotFoundException e1) {//定义file是会发生的异常
e1.printStackTrace();
} catch (IOException e) {使用数据流是会发生的异常
e.printStackTrace();
}
}
}
Upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--form表单,提交给servlet-->
<form action="UpLoadServlet" method="post"><input type="file"
name="file"> <input type="submit" value="upload"></form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Uploadtest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--servlet的注册-->
<servlet>
<servlet-name>UpLoadServlet</servlet-name>
<servlet-class>servlet.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpLoadServlet</servlet-name>
<url-pattern>/UpLoadServlet</url-pattern>
</servlet-mapping>
</web-app>
- 用Java实现 上传
- 用java实现上传文件
- 用Java实现文件上传
- plupload 上传组件,后台用java实现
- java用FTP实现上传、下载文
- java实现ftp上传
- java实现sftp上传
- java实现文件上传
- java 实现ftp上传
- java实现图片上传
- Java实现上传
- Java实现文件上传
- java 实现 图片上传
- Java实现文件上传
- java实现文件上传
- java 实现文件上传
- Java实现图片上传
- java实现文件上传
- spring bean的scope属性解决struts action单实例问题
- 后台代码里执行弹出脚本方法!(Response.Write)
- flickr api 笔记 之 获取小图
- 中文分词有关资料
- 获得占用指定端口的进程ID和进程名称
- 用Java实现 上传
- 低处的温暖
- 【CSAPP读书笔记】过程及其相关操作的分析(经典:包括函数调用过程详解)
- FCKEditor图片上传“没有权限”的问题
- 写出内存分配和释放的函数,并指出区别
- win7右键管理无法打开解决方案
- 在linux中如何编译C程序,使之成为可执行文件?如何调试?
- (转)C++界面库的抉择
- 写出socket函数,并指出其功能