文件從本地到服務(wù)器的功能,其實是為了解決目前瀏覽器不支持獲取本地文件全路徑。不得已而想到上傳到服務(wù)器的固定目錄,從而方便項目獲取文件,進而使程序支持EXCEL批量導(dǎo)入數(shù)據(jù)。
創(chuàng)新互聯(lián)長期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為集賢企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、網(wǎng)站設(shè)計,集賢網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
java中文件上傳到服務(wù)器的指定路徑的代碼:
在前臺界面中輸入:
form method="post" enctype="multipart/form-data" ?action="../manage/excelImport.do"
請選文件:input type="file" ?name="excelFile"
input type="submit" value="導(dǎo)入" onclick="return impExcel();"/
/form
action中獲取前臺傳來數(shù)據(jù)并保存
/**
* excel 導(dǎo)入文件
* @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);//保存到服務(wù)器的路徑
}
log.info("action:{} Method:{} end","usermanager","excelImport" );
return "";
}
/**
* 將MultipartFile轉(zhuǎn)化為file并保存到服務(wù)器上的某地
*/
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();
}
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.;
import org.apache.commons.net.;
public class test {
private FTPClient ftp;
/**
*
* @param path 上傳到ftp服務(wù)器哪個路徑下
* @param addr 地址
* @param port 端口號
* @param username 用戶名
* @param password 密碼
* @return
* @throws Exception
*/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
;
;
;
reply = ;
if (!FTPReply.isPositiveCompletion(reply)) {
;
return result;
}
;
result = true;
return result;
}
/**
*
* @param file 上傳的文件或文件夾
* @throws Exception
*/
private void upload(File file) throws Exception{
if(file.isDirectory()){
(file.getName());
(file.getName());
String[] files = file.list();
for (int i = 0; i files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
;
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
private static DiskFileItemFactory factory; //獲得磁盤文件條目工廠
private static ServletFileUpload upload; //文件上傳處理類
factory = new DiskFileItemFactory(); //獲得磁盤文件條目工廠
factory.setRepository(new File(config.getCache())); //創(chuàng)建緩存工廠
factory.setSizeThreshold(1024*1024*2) ; //設(shè)置緩存區(qū)的大小
upload = new ServletFileUpload(factory); //高水平的API文件上傳處理
upload.setSizeMax(10 * 1024 * 1024); //設(shè)置文件上傳的最大值
upload.setFileSizeMax(2* 1024 * 1024); //設(shè)置文件上傳的最大值
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)文件上傳
文章網(wǎng)址:http://muchs.cn/article24/docdeje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、外貿(mào)建站、定制網(wǎng)站、定制開發(fā)、企業(yè)網(wǎng)站制作、自適應(yīng)網(wǎng)站
聲明:本網(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)