---
title: "文件上传"
author: "qj200040"
date: "2010-12-28T20:03:28Z"
tags: []
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2010-12-28T20:03:28Z"
id: 854233
url: "https://qj200040.iteye.com/blog/854233"
---

HTTP 协议定义服务器端和客户端之间文件传输的沟通方式。目前HTTP协议的版本是Http1.1。RFC 2616描述了HTTP协议的具体信息。 

这个协议已经成为浏览器和Web站点之间的标准。 

当我上网的时候底层是如何进行交互的？ 

当访问者点击一个超链接的时候，将会给浏览器提交一个URL地址。通过这个URL地址，浏览器便知道去链接那个网站并去取得具体的页面文件（也可能是一张图片，一个pdf文件）。 

HTTP工作的基础就是，连接一个服务器并开始传输文件到浏览器。 

HTTP传输的基本过程 

在http传输的过程中，被称为客户端的请求者向服务器请求一个文件。 

最基本的过程是: 
1 客户端连接一个主机； 
2 服务器接收连接, 
3 客户端请求一个文件, 
4 服务器发送一个应答.

> http://zhidao.baidu.com/question/16328941.html?si=1 


struts2的xml文件

```java

<!-- 指定上传文件时临时文件存放的地方 -->
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<constant name="struts.custom.i18n.resource" value="message"></constant>
<!-- 
设置编码为GBK 
<constant name="struts.i18n.encoding" value="gbk"></constant>
-->

<package name="struts2" extends="struts-default">
	<action name="update" class="com.house.ornamentation.upload.action">
		<result name="input">/upload.jsp</result>
		<result name="success">/uploadResult.jsp</result>		
		<interceptor-ref name="fileUpload">
			<!-- 指定上传文件大小，单位为字节 -->
			<param name="maximumSize">102400000</param>
			<!-- 允许上传的文件类型，更多类型可以查看tomcat文件夹下的conf文件夹下的web.xml文件 -->
			<!-- <param name="allowedTypes">image/gif</param> -->
		</interceptor-ref>
		<interceptor-ref name="defaultStack"></interceptor-ref>
	</action>

</package>

```


java 文件应该这样子的


```java

	public String execute() throws Exception {	
		for(int i=0;i<file.size();++i){
			InputStream is= new FileInputStream(file.get(i));			
			//String root=ServletActionContext.getRequest().getRealPath("/upload");			
			//File destFile= new File(root);
			File f=new File(System.getProperty("user.dir"));			
			if(!f.isDirectory()){
				f.mkdir();
			}
			
			OutputStream os= new FileOutputStream(new File(System.getProperty("user.dir"),this.getFileFileName().get(i)));
			
			byte[]buffer=new byte[1024*1024];			
			int length=0;
			
			while((length=is.read(buffer))>0){
				os.write(buffer, 0, length);
			}			
			is.close();			
			os.close();			
		}
		return SUCCESS;
	}

jsp页面省略.....

```