发布网友
共5个回答
热心网友
Java代码实现文件上传
FormFile file=manform.getFile();
热心网友
可以用第三方工具包直接upload下,就可以了。如果非要用java
只能通过流的读取传输了。代码就不写了没有现成的代码。
热心网友
-------http://blessht.iteye.com/blog/1405057------
<%@ include file="/WEB-INF/jsp/header.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=ISO-8859-1">
<meta http-equiv="pragma" content="no-cache" />
<base target="_self">
<title>文件上传</title>
</head>
<body>
<h5>文件上传</h5><hr/>
<form id="file_upload_id" name="file_upload_name" action="<%=root%>/CommonController.jhtml?method=doFileUpload" method="post" enctype="multipart/form-data">
<input type="hidden" name="functionId" value="${functionId}"/>
<input type="hidden" name="fileType" value="${fileType}"/>
<input type="hidden" name="maxSize" value="${maxSize}"/>
<div><input type="file" name="file_upload"/></div>
<c:if test="${maxSize!=null}">
<div style="font: 12">文件最大不能超过${maxSize}MB</div>
</c:if>
<c:if test="${fileType!=null}">
<div style="font: 12">文件格式必须是:${fileType}</div>
</c:if>
<div><input type="submit" value="上传"/></div>
</form>
</body>
</html>
热心网友
分真少。。。
public static int transFile(InputStream in, OutputStream out, int fileSize) {
int receiveLen = 0;
final int bufSize = 1000;
try {
byte[] buf = new byte[bufSize];
int len = 0;
while(fileSize - receiveLen > bufSize)
{
len = in.read(buf);
out.write(buf, 0, len);
out.flush();
receiveLen += len;
System.out.println(len);
}
while(receiveLen < fileSize)
{
len = in.read(buf, 0, fileSize - receiveLen);
System.out.println(len);
out.write(buf, 0, len);
receiveLen += len;
out.flush();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return receiveLen;
}
这个方法从InputStream中读取内容,写到OutputStream中。
那么发送文件方,InputStream就是FileInputStream,OutputStream就是Socket.getOutputStream.
接受文件方,InputStream就是Socket.getInputStream,OutputStream就是FileOutputStream。
就OK了。 至于存到数据库里嘛,Oracle里用Blob。搜索一下,也是一样的。从Blob能获取一个输出流。
热心网友
commons-fileupload.jar
第三方jar包