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

html上传原理

发布时间: 2023-03-12 20:24:21

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

我借助别人的资料,感觉挺详细,仔细研究一下吧
中文件上传与下载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里input标签里的file控件的实现原理

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

㈢ 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(临时路径名附件,真实路径名附件)

㈣ html是怎么和数据库进行交互的怎么把值传到数据库,或者数据库是怎么把值传到页面的

数据库方面的东西是服务端程序员该做的事情,一般前端不需要深入了解。当然,对于一个合格的前端来说,多多了解服务端的工作原理还有很有必要的。下面我就来说说自己的理解吧。
html本身是不具备与数据库交互的能力的,服务端一般要一个服务器软件,比如iis、Apache,再搭配一门服务端的语言,比如java、php等,与数据库的交互是通过服务端语言进行的,服务端语言通过向数据库发送sql指令完成数据的增删改查等工作,如果是读取数据,就把读取到的数据经过一定的处理后返回html页面。过程大致如此,有不理解的可以继续交流。

㈤ http中上传文件的原理

http中上传文件的原理如下:
在最初的http协议中,没有上传文件方面的功能。 rfc1867 ( http://www.ietf.org/rfc/rfc1867.txt ) 为 http 协议添加了这个功能。客户端的浏览器,如 Microsoft IE, Mozila, Opera 等,按照此规范将用户指定的文件发送到服务器。服务器端的网页程序,如 php, asp, jsp 等,可以按照此规范,解析出用户发送来的文件。Microsoft IE, Mozila, Opera 已经支持此协议,在网页中使用一个特殊的 form 就可以发送文件。绝大部分 http server ,包括 tomcat ,已经支持此协议,可接受发送来的文件。各种网页程序,如 php, asp, jsp 中,对于上传文件已经做了很好的封装。

超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。1960年美国人Ted Nelson构思了一种通过计算机处理文本信息的方法,并称之为超文本(hypertext),这成为了HTTP超文本传输协议标准架构的发展根基。

热点内容
内置存储卡可以拆吗 发布:2025-05-18 04:16:35 浏览:335
编译原理课时设置 发布:2025-05-18 04:13:28 浏览:378
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:612
java用什么软件写 发布:2025-05-18 03:56:19 浏览:32
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:107
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:943
安卓手机如何拍视频和苹果一样 发布:2025-05-18 03:40:47 浏览:739
为什么安卓手机连不上苹果7热点 发布:2025-05-18 03:40:13 浏览:802
网卡访问 发布:2025-05-18 03:35:04 浏览:510
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:371