新聞中心
java中怎樣上傳文件
Java代碼實現(xiàn)文件上傳

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設與策劃設計,五龍口網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十多年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:五龍口等地區(qū)。五龍口做網(wǎng)站價格咨詢:18982081108
FormFile?file=manform.getFile();?
String?newfileName?=?null;
String?newpathname=null;
String?fileAddre="/numUp";
try?{
InputStream?stream?=?file.getInputStream();//?把文件讀入
String?filePath?=?request.getRealPath(fileAddre);//取系統(tǒng)當前路徑
File?file1?=?new?File(filePath);//添加了自動創(chuàng)建目錄的功能
((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();
}
java中怎么把文件上傳到服務器的指定路徑?
文件從本地到服務器的功能,其實是為了解決目前瀏覽器不支持獲取本地文件全路徑。不得已而想到上傳到服務器的固定目錄,從而方便項目獲取文件,進而使程序支持EXCEL批量導入數(shù)據(jù)。
java中文件上傳到服務器的指定路徑的代碼:
在前臺界面中輸入:
form method="post" enctype="multipart/form-data" ?action="../manage/excelImport.do"
請選文件:input type="file" ?name="excelFile"
input type="submit" value="導入" onclick="return impExcel();"/
/form
action中獲取前臺傳來數(shù)據(jù)并保存
/**
* excel 導入文件
* @return
* @throws IOException
*/
@RequestMapping("/usermanager/excelImport.do")
public String excelImport(
String filePath,
MultipartFile ?excelFile,HttpServletRequest request) throws IOException{
log.info("action:{} Method:{} start","usermanager","excelImport" );
if (excelFile != null){
String filename=excelFile.getOriginalFilename();
String a=request.getRealPath("u/cms/www/201509");
SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename);//保存到服務器的路徑
}
log.info("action:{} Method:{} end","usermanager","excelImport" );
return "";
}
/**
* 將MultipartFile轉(zhuǎn)化為file并保存到服務器上的某地
*/
public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException
{ ? ?
FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);
System.out.println("------------"+path + "/"+ savefile);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
Java中fileupload上傳文件的代碼
private static DiskFileItemFactory factory; //獲得磁盤文件條目工廠
private static ServletFileUpload upload; //文件上傳處理類
factory = new DiskFileItemFactory(); //獲得磁盤文件條目工廠
factory.setRepository(new File(config.getCache())); //創(chuàng)建緩存工廠
factory.setSizeThreshold(1024*1024*2) ; //設置緩存區(qū)的大小
upload = new ServletFileUpload(factory); //高水平的API文件上傳處理
upload.setSizeMax(10 * 1024 * 1024); //設置文件上傳的最大值
upload.setFileSizeMax(2* 1024 * 1024); //設置文件上傳的最大值
ListFileItem list = upload.parseRequest(request);
for(FileItem item : list){
String fieldName = item.getFieldName(); //獲取表單的屬性名字
String fileName = item.getName() ; //獲取文件名
if(item.isFormField()){ //如果獲取的 表單信息是普通的 文本 信息
}else{
File file = new File("d://test.txt");
item.write(file);
}
}
文章名稱:java中上傳文件代碼 java代碼實現(xiàn)文件上傳
轉(zhuǎn)載注明:http://www.dlmjj.cn/article/dosegpe.html


咨詢
建站咨詢
