A. form表单怎么能直接提交ftp服务器上,把文件上传到ftp服务器上!
没办法直接传的!
你只能用表单先传到web服务器上,
然后再从web服务器上传到FTP上!(可以自己写一个服务程序)
B. springmvc 怎么将文件上传到linux服务器
Spring MVC为文件上传提供了直接的支持,这种支持是通过即插即用的MultipartResolver实现的。Spring使用Jakarta Commons FileUpload 技术实现了一个MultipartResolver实现类:CommonsMultipartResolver。
Spring MVC上下文中默认没有装配MultipartResolver,因此默认情况下不能处理文件的上传工作。如果想要使用Spring的文件上传功能,需要先在上下文中配置MultipartResolver。
第一步:配置MultipartResolver
使用CommonsMultipartResolver配置一个MultipartResolver解析器:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8"
p:maxUploadSize="5242880"
p:uploadTempDir="upload/temp"
/>
defaultEncoding必须和用户JSP的pageEncoding属性一致,以便正确读取表单的内容。uploadTempDir是文件上传过程所使用的临时目录,文件上传完成后,临时目录中的临时文件会被自动清除。
第二步:编写文件上传表单页面和控制器
JSP页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>My JSP 'index.jsp' starting page</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">
</head>
<body>
<h1>选择上传文件</h1>
<form action="<%=basePath%>user/upload.do" method="post" enctype="multipart/form-data">
文件名:<input type="text" name="name" /><br/>
<input type="file" name="file" /><br/>
<input type="submit" />
</form>
</body>
</html>
注意:负责上传的表单和一般表单有一些区别,表单的编码类型必须是"Multipart/form-data"
控制器UserController如下:
package com.web;
import java.io.File;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping(value = "/upload.do")
public String updateThumb(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file)
throws IllegalStateException, IOException {
if (!file.isEmpty()) {
file.transferTo(new File("d:/temp/"
+ name
+ file.getOriginalFilename().substring(
file.getOriginalFilename().lastIndexOf("."))));
return "redirect:success.html";
} else {
return "redirect:fail.html";
}
}
}
Spring MVC会将上传文件绑定到MultipartFile对象中。MultipartFile提供了获取上传文件内容、文件名等内容,通过transferTo方法还可将文件存储到硬件中,具体说明如下:
byte[] getBytes() :获取文件数据
String getContentType():获取文件MIME类型,如image/pjpeg、text/plain等
InputStream getInputStream():获取文件流
String getName():获取表单中文件组件的名字
String getOriginalFilename():获取上传文件的原名
long getSize():获取文件的字节大小,单位为byte
boolean isEmpty():是否有上传的文件
void transferTo(File dest):可以使用该文件将上传文件保存到一个目标文件中
源码:uploadtest.zip
C. 如何使用multipart/form-data格式上传文件
在网络编程过程中需要向服务器上传文件。Multipart/form-data是上传文件的一种方式。
Multipart/form-data其实就是浏览器用表单上传文件的方式。最常见的情境是:在写邮件时,向邮件后添加附件,附件通常使用表单添加,也就是用multipart/form-data格式上传到服务器。
表单形式上传附件
具体的步骤是怎样的呢?
首先,客户端和服务器建立连接(TCP协议)。
第二,客户端可以向服务器端发送数据。因为上传文件实质上也是向服务器端发送请求。
第三,客户端按照符合“multipart/form-data”的格式向服务器端发送数据。
既然Multipart/form-data格式就是浏览器用表单提交数据的格式,我们就来看看文件经过浏览器编码后是什么样子。
这行指出这个请求是“multipart/form-data”格式的,且“boundary”是 “---------------------------7db15a14291cce”这个字符串。
不难想象,“boundary”是用来隔开表单中不同部分数据的。例子中的表单就有 2 部分数据,用“boundary”隔开。“boundary”一般由系统随机产生,但也可以简单的用“-------------”来代替。
实际上,每部分数据的开头都是由"--" + boundary开始,而不是由 boundary 开始。仔细看才能发现下面的开头这段字符串实际上要比 boundary 多了个 “--”
紧接着 boundary 的是该部分数据的描述。
接下来才是数据。
“GIF”gif格式图片的文件头,可见,unknow1.gif确实是gif格式图片。
在请求的最后,则是 "--" + boundary + "--" 表明表单的结束。
需要注意的是,在html协议中,用 “ ” 换行,而不是 “ ”。
下面的代码片断演示如何构造multipart/form-data格式数据,并上传图片到服务器。
//---------------------------------------
// this is the demo code of using multipart/form-data to upload text and photos.
// -use WinInet APIs.
//
//
// connection handlers.
//
HRESULT hr;
HINTERNET m_hOpen;
HINTERNET m_hConnect;
HINTERNET m_hRequest;
//
// make connection.
//
...
//
// form the content.
//
std::wstring strBoundary = std::wstring(L"------------------");
std::wstring wstrHeader(L"Content-Type: multipart/form-data, boundary=");
wstrHeader += strBoundary;
HttpAddRequestHeaders(m_hRequest, wstrHeader.c_str(), DWORD(wstrHeader.size()), HTTP_ADDREQ_FLAG_ADD);
//
// "std::wstring strPhotoPath" is the name of photo to upload.
//
//
// uploaded photo form-part begin.
//
std::wstring strMultipartFirst(L"--");
strMultipartFirst += strBoundary;
strMultipartFirst += L" Content-Disposition: form-data; name="pic"; filename=";
strMultipartFirst += L""" + strPhotoPath + L""";
strMultipartFirst += L" Content-Type: image/jpeg ";
//
// "std::wstring strTextContent" is the text to uploaded.
//
//
// uploaded text form-part begin.
//
std::wstring strMultipartInter(L" --");
strMultipartInter += strBoundary;
strMultipartInter += L" Content-Disposition: form-data; name="status" ";
std::wstring wstrPostDataUrlEncode(CEncodeTool::Encode_Url(strTextContent));
// add text content to send.
strMultipartInter += wstrPostDataUrlEncode;
std::wstring strMultipartEnd(L" --");
strMultipartEnd += strBoundary;
strMultipartEnd += L"-- ";
//
// open photo file.
//
// ws2s(std::wstring)
// -transform "strPhotopath" from unicode to ansi.
std::ifstream *pstdofsPicInput = new std::ifstream;
pstdofsPicInput->open((ws2s(strPhotoPath)).c_str(), std::ios::binary|std::ios::in);
pstdofsPicInput->seekg(0, std::ios::end);
int nFileSize = pstdofsPicInput->tellg();
if(nPicFileLen == 0)
{
return E_ACCESSDENIED;
}
char *pchPicFileBuf = NULL;
try
{
pchPicFileBuf = new char[nPicFileLen];
}
catch(std::bad_alloc)
{
hr = E_FAIL;
}
if(FAILED(hr))
{
return hr;
}
pstdofsPicInput->seekg(0, std::ios::beg);
pstdofsPicInput->read(pchPicFileBuf, nPicFileLen);
if(pstdofsPicInput->bad())
{
pstdofsPicInput->close();
hr = E_FAIL;
}
delete pstdofsPicInput;
if(FAILED(hr))
{
return hr;
}
// Calculate the length of data to send.
std::string straMultipartFirst = CEncodeTool::ws2s(strMultipartFirst);
std::string straMultipartInter = CEncodeTool::ws2s(strMultipartInter);
std::string straMultipartEnd = CEncodeTool::ws2s(strMultipartEnd);
int cSendBufLen = straMultipartFirst.size() + nPicFileLen + straMultipartInter.size() + straMultipartEnd.size();
// Allocate the buffer to temporary store the data to send.
PCHAR pchSendBuf = new CHAR[cSendBufLen];
memcpy(pchSendBuf, straMultipartFirst.c_str(), straMultipartFirst.size());
memcpy(pchSendBuf + straMultipartFirst.size(), (const char *)pchPicFileBuf, nPicFileLen);
memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen, straMultipartInter.c_str(), straMultipartInter.size());
memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen + straMultipartInter.size(), straMultipartEnd.c_str(), straMultipartEnd.size());
//
// send the request data.
//
HttpSendRequest(m_hRequest, NULL, 0, (LPVOID)pchSendBuf, cSendBufLen)
D. php上传文件到服务器
1、通过PHP,可以把文件上传到服务器。创建一个文件上传表单,下面这个供上传文件的 HTML 表单:
<html>
<body>
<formaction="upload_file.php"method="post"
enctype="multipart/form-data">
<labelfor="file">Filename:</label>
<inputtype="file"name="file"id="file"/>
<br/>
<inputtype="submit"name="submit"value="Submit"/>
</form>
</body>
</html>
2、创建上传脚本,命名为"upload_file.php" 文件含有供上传文件的代码:
<?php
if($_FILES["file"]["error"]>0)
{
echo"Error:".$_FILES["file"]["error"]."<br/>";
}
else
{
echo"Upload:".$_FILES["file"]["name"]."<br/>";
echo"Type:".$_FILES["file"]["type"]."<br/>";
echo"Size:".($_FILES["file"]["size"]/1024)."Kb<br/>";
echo"Storedin:".$_FILES["file"]["tmp_name"];
}
?>
注:通过使用 PHP 的全局数组 “$_FILES”,就可以实现从客户计算机向远程服务器上传文件。