当前位置:首页 » 文件管理 » html上传文件原理

html上传文件原理

发布时间: 2022-11-12 17:09:56

㈠ 求高手指教,网页上传文件是如何实现的

其实有时候答案应该自己去寻找,你既然知道做法,直接抓个包不就知道流程了吗,

  1. 选择文件之后,浏览器只是得到了文件的路径,就是你看到的文本框里的值.

  2. 点击上传后,会给服务器发送类似以下格式的封包,这是原始封包,服务器收到之后,会进行解析才会到达动态脚本,比如php,

以下是我上传了一张桌面上的placeholder.gif图片文件,所发送给服务器的封包.里面包括了文件的一些信息和内容.你看下.他们用HTTP头信息里的分隔符分开.

POST /up.php HTTP/1.1

Accept:*/*

Referer: http://www.xxx.com/

Accept-Language: zh-cn

Content-Type: multipart/form-data; boundary=---------------------------7df2ef0c00e4

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

Host: www.xxx.com

Content-Length: 10810

Connection: Keep-Alive

Cache-Control: no-cache

Cookie: lang=zh-cn; _ga=GA1.2.447688366.1439125779; _gat=1; Hm_lvt_=1439125779; Hm_lpvt_=1439125779

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="UPLOAD_IDENTIFIER"

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="langkey"

1

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="setcookie"

1

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="tempvar"

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="upfile"; filename="C:Documents and SettingsAdministrator......placeholder.gif"

Content-Type: image/gif

(文件二进制数据)

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="fpath"

C:Documents and SettingsAdministrator......placeholder.gif

-----------------------------7df2ef0c00e4--

㈡ html中怎么上传文件代码

在HTML标准中,XMLHttpRequest对象被重新定义,被称为“XMLHttpRequest Level 2”,其中包含了以下5个新特性:

1、支持上传、下载字节流,比如文件、blob以及表单数据。

2、增加了上传、下载中的进度事件。

3、跨域请求的支持。

4、允许发送匿名请求(即不发送HTTP的Referer部分)。

5、允许设置请求的超时。

    在这篇教程中,我们主要关注第一和第二项特性,尤其是第二项——它能够提供我们想要的上传进度。和之前的方案不同,这个方案并不要求服务器作出特殊的设置,因此大家边看教程就可以边动手试试了。

    上面图示的就是我们能够实现的内容:

    1、显示上传的文件信息,比如文件名、类型、尺寸。

    2、一个能够显示真实进度的进度条。

    3、上传的速度。

    4、剩余时间的估算。

    5、已上传的数据量。

    6、上传结束后服务器返回的响应。

    另外,凭借XMLHttpRequest,我们的上传过程整个都是异步的,因此用户在上传文件的时候,依然可以操作网页当中的其它元素,并不需要专门等待上传的完成。而在上传结束后,我们能够获取服务器发回的响应,因此整个上传过程都显得相当顺理成章。

㈢ 文件上传的基本原理,用文字详细说明一下

我借助别人的资料,感觉挺详细,仔细研究一下吧
中文件上传与下载1。上传文件流程

先通过用户提交文件,保存文件到服务器端,然后在写入数据库中,每次到下载页面时从数据库中读出文件,生成文件在服务器目录中,以下。。。

文件上传后保存文件到服务器中jsp,

upFile.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" %>
< import="java.util.List;"%>

<html>
<head>
<title>上传文件</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<base target="_self">
<link href="Css/style.css" rel="stylesheet">
<script type="text/javascript">
function check(){
var file = document.getElementById("file1").value;
if(file == ""){
alert('请选择要上传的文件');
return false;
}else{
window.returnValue=file;
return true;
}
}
</script>
</head>
<body>
<%
String files=request.getParameter("files");
%>
<form name="form2" enctype="multipart/form-data" method="post" action="../MissionManage/commonfile.jsp?files=<%=files %>" onsubmit="return check();">
<center>
<table align="center" width="350" height="150" border="0" cellpadding="0" cellspacing="0"><!-- background="images/upFile_bg.gif -->
<tr>
<td valign="top"><table width="100%" height="145" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="49" colspan="2"></td>
</tr>
<tr>
<td width="9%" height="53"></td>
<td width="91%"><b>请选择上传的文件:</b><br>
<input id="file1" name="file1" type="file" size="35" onkeydown="return false;">
<br>
注:文件大小请控制在10M以内。</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="Submit" type="submit" class="btn_grey" value="确认">

<input name="Submit2" type="button" class="btn_grey" onClick="window.close()" value="关闭"></td>
</tr>
</table></td>
</tr>
</table>
</center>
</form>
</body>
</html>

commonfile.jsp

<%@ page language="java" import="java.io.*" pageEncoding="UTF-8"%>
< import="java.util.*,org.apache.commons.fileupload.FileItem" %>
< import="java.text.SimpleDateFormat,java.util.Date" %>
< import="com.PoliceSystem.tools.FileOperate" %>
<jsp:useBean id="factory" scope="page" class="org.apache.commons.fileupload.disk.DiskFileItemFactory" />
<jsp:useBean id="upload" scope="page" class="org.apache.commons.fileupload.servlet.ServletFileUpload" />
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>上传文件</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<base target="_self">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" language="javascript">

</script>
</head>

<body>
<%
request.setCharacterEncoding("UTF-8");
String path1 = (String)request.getRealPath("/upload1");

String files=request.getParameter("files");
String[] fileList=files.split(";");

File file = new File(path1);

if(!file.exists()){
file.mkdirs();
}
factory.setRepository(file);
factory.setSizeThreshold(1024*1024);

upload.setFileItemFactory(factory);

try{
List<FileItem> list= upload.parseRequest(request);

for(FileItem item:list){
if(item.isFormField()){
//String value=item.getString("UTF-8");
//session.setAttribute("fileName", value);
}else{
String value=item.getName();
int start=value.lastIndexOf("\\");
String fileName=value.substring(start+1);
if(fileName.length() == 0 || fileName == ""){
out.println("<script>alert('上传失败');window.close();</script>");
}else{
if(item.getSize()>10000000){
out.println("<script>alert('对不起,您上传的文件超过10M,无法完成上传!');window.close();</script>");
}else{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date =new Date();
String[] f = item.getName().split("\\\\");
//System.out.println(f[f.length-1]);
String oldFileName = f[f.length-1];
for(int i=0;i<fileList.length;i++){
if(fileList[i].indexOf(oldFileName)!=-1){
out.println("<script>alert('对不起,您上传的文件与现有上传的文件重名,请更换文件名重新上传!');window.returnValue='';window.close();</script>");
}
}
String fType = FileOperate.getFileType(item.getName());//文件类型
fileName = sdf.format(date)+"."+fType;//新文件名
//System.out.println(item.getName()+"---"+fileName);
item.write(new java.io.File(path1,fileName));
//////--1--/////
String mailFileNames = new String();
String old = (String)session.getAttribute("fuJianFileNames");
if(old!=null){
mailFileNames = old;
}
mailFileNames+=oldFileName+"|"+fileName+";";
//System.out.println("mailFileNames="+mailFileNames);
session.removeAttribute("fuJianFileNames");
session.setAttribute("fuJianFileNames", mailFileNames);
//////--2--/////
//String pathName = path1+";
//System.out.println("pathName="+pathName);
out.println("<script>window.close();</script>");
}
}
}
}
}catch (Exception e) {
e.printStackTrace();
out.println("<script>window.close();</script>");
}
%>
</body>
</html>

插入文件数据到数据库中

public boolean insert_annex(String[] str){
boolean b=true;
String sql ="";
con = db.getConn();
try{
sql="insert into annex (querykey,sfilename,committime,Filetype,filepath,pno,Annex) values (?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
for(int i=0;i<str.length-1;i++){
ps.setString(i+1, str[i]);
}
File file = new File(str[6]);//附件

InputStream iso = new FileInputStream(file);
ps.setBinaryStream(7, iso, iso.available());
ps.execute();
iso.close();
System.out.println("删除临时文件:" + file.delete());// 删除tmp文件
}catch(Exception e){
b=false;
e.printStackTrace();
}finally{
this.close();
}
return b;
}

读出文件数据,并保存

public boolean addTempFile_annex(String querykey,HttpServletRequest request){
boolean b=true;
con = db.getConn();
InputStream in = null;
OutputStream out= null;
String path1 = (String)request.getRealPath("/upload1");//文件下载临时目录
try{
String sql="select filePath,annex from annex where querykey='"+querykey+"'";
//String sql_1="select annex from annex where querykey='"+querykey+"'";

ps = con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
File f = new File(path1+rs.getString("filepath"));
in=rs.getBinaryStream("annex");
out = new FileOutputStream(f);
int len = 10 * 100 * 100; //定义字符数组长度
byte[] P_Buf=new byte[len];
int j;
while((j=in.read(P_Buf))!=-1){
out.write(P_Buf, 0, j);
}
}
in.close();
out.flush(); //强制清出缓冲区
out.close();
}catch(Exception e){
b=false;
e.printStackTrace();
}finally{
this.close();
}
return b;
}

给定传递过来的参数(文件名,文件存储在服务器的文件名,文件在服务器的路径,文件类型),下载文件Action

package com.PoliceSystem.action.mail;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class downLoadFile extends ActionSupport{
private static final long serialVersionUID = -2207648627734251737L;

public String execute() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();

String fileminitype = request.getParameter("fileType");
String filename1 = new String(request.getParameter("fileName1").getBytes("ISO8859-1"),"UTF-8");
String filename2 = new String(request.getParameter("fileName2").getBytes("ISO8859-1"),"UTF-8");
String filepath = request.getRealPath("/upload1");
File f = new File(filepath+");
Long filelength = f.length();
int cacheTime = 10;

response.setContentType(fileminitype);
response.setHeader("Location",filename1);
response.setHeader("Cache-Control", "max-age=" + cacheTime);

response.setContentType("application/octet-stream");
byte[] b = filename1.getBytes("GBK");
filename1 = new String(b,"8859_1");
response.setHeader("Content-Disposition", "attachment;filename=" + filename1);
response.setContentLength(filelength.intValue());
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(f);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}

outputStream.flush();
outputStream.close();
inputStream.close();

return null ;
}
}

㈣ html网页如何做到上传文件

HTML只能做表单。要上传到服务器,还必须要服务器端执行的语句文件。如:ASP、PHP等。

㈤ http文件上传的原理是什么请问向qq邮箱这类可以上传文件的网页,他们上传文件的原理是不是,当用

有个误区:
在上传插件中,用户点击提交(确认)后,文件才开始上传,这时候,会上传到服务器的某个目录(不是临时目录,一般是服务器按照管理用户文件的规则所建立的文件夹),服务器会返回一个指针,这个指针指向了你上传的文件。当你发送邮件后,邮件中也会包含这个指针。不管你把这个文件放到哪个文件夹,实际上都只是存放的这个指针而已。如非管理需要,这个文件应该会永久保存,任何转发,转储的行为,其实也只是操作的这个指针而不是文件本身。
即使你删除文件,也只是在你的文件夹中删除这个指针。所以,很多“极速秒传”实际上是先判断你要上传的文件的MD5值是否已经在服务器中存在,如果存在,就把已存在的文件的指针放上去,是不是很快?

㈥ HTML静态网页中怎么实现上传文件功能,需要判断后缀名!

必须使用编程语言,例如php、asp、asp.net或者jsp之类的。

㈦ html中input上传图片什么原理啊php后台怎么处理如果用ajax的话是传些什么

用input上传图片是把图片作为文件传输的,在php后台中使用 $_FILES来接收。

注意:前端的form表单除了action ,method 属性外,还要添加一个'enctype'属性,否则文件传输不成功。

<form enctype="multipart/form-data">

<input type="file" >

</form>

$_FILES接收信息 有几个属性:

name , 上传的文件名称

size ,上传的文件大小

tmp_name ,临时路径

type ,文件类型

error错误提示

error取值说明:

( 0:没问题。1/2:大小超过限制[1->超出php.ini限制。2->超出文件域max_file限制]。3:只上传部分附件(不好测试)。4:没有上传附件)

有上传信息时:$_FILES接收到的附件信息:


保存附件:把上传的文件由临时路径保存到真实的图片存储的位置。

move_uploaded_file(临时路径名附件,真实路径名附件)

㈧ http文件上传的原理

HTTP 协议定义服务器端和客户端之间文件传输的沟通方式。目前HTTP协议的版本是Http1.1。RFC 2616描述了HTTP协议的具体信息。

这个协议已经成为浏览器和Web站点之间的标准。

当我上网的时候底层是如何进行交互的?

访问者点击一个超链接的时候,将会给浏览器提交一个URL地址。通过这个URL地址,浏览器便知道去链接那个网站并去取得具体的页面文件(也可能是一张图片,一个pdf文件)。

HTTP工作的基础就是,连接一个服务器并开始传输文件到浏览器。

HTTP传输的基本过程

在http传输的过程中,被称为客户端的请求者向服务器请求一个文件。

最基本的过程是:
1 客户端连接一个主机;
2 服务器接收连接,
3 客户端请求一个文件,
4 服务器发送一个应答.

㈨ html有关多个文件上传

//改好了没问题..加文件试试
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script type="text/javascript">

function imgChange(next) {
if(next != null)
document.getElementById(next).style.display = "";
}

function validate() {
var phos = 2;
for(i = 0; i < 2; i++) {
if($("del" + i).checked == true)
phos--;
}
for(i = 0; i < 4; i++) {
if($("photoFile" + i).value != "")
phos++;
}
if(phos > 4) {
alert("图片太多,您最多总共可以保存4张图片!");
return false;
}
return true;
}
</script>

<form action="/addTrade.do" method="post" enctype="multipart/form-data" onSubmit="return validate()">
<!-- 上传照片-->
<div>

<table width="100%" border="0" cellspacing="0" cellpadding="0"
summary="upload pictures">
<tr id="tr_photoFile0">
<td width="119" align="right" class="title">
上传照片:
</td>

<td width="499">
<input type="file" name="photoFile0" id="photoFile0"
size="40" onChange='return imgChange("tr_photoFile1")' />
</td>
</tr>
<tr id="tr_photoFile1" style="display: none;">
<td>

</td>
<td>

<input type="file" name="photoFile1" id="photoFile1"
onChange='return imgChange("tr_photoFile2")' size="40" />
</td>
</tr>
<tr id="tr_photoFile2" style="display: none;">
<td>

</td>
<td>
<input type="file" name="photoFile2" id="photoFile2"
onChange='return imgChange("tr_photoFile3")' size="40" />

</td>
</tr>
<tr id="tr_photoFile3" style="display: none;">
<td>

</td>
<td>
<input type="file" name="photoFile3" id="photoFile3"
onChange='return imgChange(null)' size="40" />
</td>

</tr>
</table>
</div>

<!--/ 上传照片-->
<input name="submit" type="submit" value="提交"/>
</form>

㈩ html里input标签里的file控件的实现原理

不需要建立
临时文件
,file
控件
本身只是获取一下你要上传文件的路径,至于上传就属于
后台
操作了。

热点内容
想买电脑配置要注意哪些 发布:2024-10-06 11:21:50 浏览:541
滴滴云存储 发布:2024-10-06 11:17:37 浏览:765
精通android游戏开发 发布:2024-10-06 11:16:54 浏览:800
ip网络域名服务器 发布:2024-10-06 11:14:06 浏览:642
内部接口java 发布:2024-10-06 11:13:10 浏览:114
phpsession共享 发布:2024-10-06 11:02:28 浏览:100
彩票源码下载 发布:2024-10-06 10:56:41 浏览:368
mysql文件导入数据库文件 发布:2024-10-06 10:56:39 浏览:910
fpga编程语言 发布:2024-10-06 10:29:24 浏览:343
python按时间排序 发布:2024-10-06 10:02:50 浏览:216