首页 养生问答 疾病百科 养生资讯 女性养生 男性养生

java中怎样上传文件

发布网友

我来回答

5个回答

热心网友

Java代码实现文件上传

FormFile file=manform.getFile(); 
  String newfileName = null;
  String newpathname=null;
  String fileAddre="/numUp";
  try {
   InputStream stream = file.getInputStream();// 把文件读入
    String filePath = request.getRealPath(fileAddre);//取系统当前路径
          File file1 = new File(filePath);//添加了自动创建目录的功能
       ((File) file1).mkdir();   
    newfileName = System.currentTimeMillis()
     + file.getFileName().substring(
       file.getFileName().lastIndexOf('.'));
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   OutputStream bos = new FileOutputStream(filePath + "/"
     + newfileName);
   newpathname=filePath+"/"+newfileName;
   System.out.println(newpathname);
   // 建立一个上传文件的输出流
    System.out.println(filePath+"/"+file.getFileName());
   int bytesRead = 0;
   byte[] buffer = new byte[8192];
   while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
    bos.write(buffer, 0, bytesRead);// 将文件写入服务器
   }
   bos.close();
   stream.close();
    } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

热心网友

可以用第三方工具包直接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包

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com