public static int transFile(InputStream in, OutputStream out, int fileSize) {

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比湟中網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式湟中網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋湟中地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。
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中讀取內(nèi)容,寫到OutputStream中。
那么發(fā)送文件方,InputStream就是FileInputStream,OutputStream就是Socket.getOutputStream.
接受文件方,InputStream就是Socket.getInputStream,OutputStream就是FileOutputStream。
就OK了。 至于存到數(shù)據(jù)庫里嘛,Oracle里用Blob。搜索一下,也是一樣的。從Blob能獲取一個輸出流。
下面是我用過的一段代碼,fileupload、servlet搞的
DiskFileItemFactory factory = new DiskFileItemFactory();//為該請求創(chuàng)建一個DiskFileItemFactory對象,通過它來解析請求。執(zhí)行解析后,所有的表單項目都保存在一個List中。
factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(-1);
upload.setHeaderEncoding("UTF-8");
ListFileItem items;//對應(yīng)jsp表單的域
File icon = null;//這是我要保存的文件,是一個icon
try {
items = upload.parseRequest(request);//通過request獲得請求表單的域
if(items!=nullitems.size()0){
IteratorFileItem itr = items.iterator();
while(itr.hasNext()){
FileItem item = (FileItem) itr.next();
if(item.isFormField()){
//普通表單域
String fieldName = item.getFieldName();//表單域的name屬性
String value = item.getString("UTF-8");//表單域的value或者textarea的內(nèi)容
if("news.title".equals(fieldName)){
news.setTitle(value);
}
...
}else{
//如果不是普通的表單域,即文本域
String fieldName = item.getFieldName();//文本域的name屬性
String value = item.getName();//文件名
if("icon".equals(fieldName)){
if(!StringUtils.isEmpty(value)){
String filename = String.valueOf(UUID.randomUUID());
new File(WinWinConstant.file_path+File.separatorChar+WinWinConstant.NEWS).mkdirs();
//設(shè)置icon保存的路徑
icon = new File(WinWinConstant.file_path+File.separatorChar+WinWinConstant.NEWS + File.separatorChar + filename+value.substring(value.lastIndexOf('.')));
item.write(icon);//保存文件
}
}
...
}
}
}
}catch(Exception e){
e.printStackTrace();
}
common-fileupload是jakarta項目組開發(fā)的一個功能很強(qiáng)大的上傳文件組件
下面先介紹上傳文件到服務(wù)器(多文件上傳):
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import org.apache.commons.fileupload.*;
public class upload extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out=response.getWriter();
try {
DiskFileUpload fu = new DiskFileUpload();
// 設(shè)置允許用戶上傳文件大小,單位:字節(jié),這里設(shè)為2m
fu.setSizeMax(2*1024*1024);
// 設(shè)置最多只允許在內(nèi)存中存儲的數(shù)據(jù),單位:字節(jié)
fu.setSizeThreshold(4096);
// 設(shè)置一旦文件大小超過getSizeThreshold()的值時數(shù)據(jù)存放在硬盤的目錄
fu.setRepositoryPath("c://windows//temp");
//開始讀取上傳信息
List fileItems = fu.parseRequest(request);
// 依次處理每個上傳的文件
Iterator iter = fileItems.iterator();
//正則匹配,過濾路徑取文件名
String regExp=".+////(.+)$";
//過濾掉的文件類型
String[] errorType={".exe",".com",".cgi",".asp"};
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem)iter.next();
//忽略其他不是文件域的所有表單信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) size==0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result){
for (int temp=0;tempERRORTYPE.LENGTH;TEMP++){
if (m.group(1).endsWith(errorType[temp])){
throw new IOException(name+": wrong type");
}
}
try{
//保存上傳的文件到指定的目錄
//在下文中上傳文件至數(shù)據(jù)庫時,將對這里改寫
item.write(new File("d://" + m.group(1)));
out.print(name+" ?"+size+"");
}
catch(Exception e){
out.println(e);
}
}
else
{
throw new IOException("fail to upload");
}
}
}
}
catch (IOException e){
out.println(e);
}
catch (FileUploadException e){
out.println(e);
}
}
}
現(xiàn)在介紹上傳文件到服務(wù)器,下面只寫出相關(guān)代碼:
以sql2000為例,表結(jié)構(gòu)如下:
字段名:name ? ?filecode
類型: varchar ? ? image
數(shù)據(jù)庫插入代碼為:PreparedStatement pstmt=conn.prepareStatement("insert into test values(?,?)");
代碼如下:
。。。。。。
try{
這段代碼如果不去掉,將一同寫入到服務(wù)器中
//item.write(new File("d://" + m.group(1)));
int byteread=0;
//讀取輸入流,也就是上傳的文件內(nèi)容
InputStream inStream=item.getInputStream(); ? ? ? ? ? ?
pstmt.setString(1,m.group(1));
pstmt.setBinaryStream(2,inStream,(int)size);
pstmt.executeUpdate();
inStream.close();
out.println(name+" ?"+size+" ");
}
。。。。。。
這樣就實現(xiàn)了上傳文件至數(shù)據(jù)庫
分享文章:java代碼實現(xiàn)上傳功能,java上傳文件實現(xiàn)
地址分享:http://chinadenli.net/article29/dsipoch.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、App設(shè)計、網(wǎng)站排名、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)