當前位置:首頁 » 編程語言 » java傳地址

java傳地址

發布時間: 2023-09-13 23:32:47

1. 怎麼用java程序實現上傳文件到指定的URL地址

//保存圖片
privatevoidsaveImg(HttpServletRequestrequest,FormFileimgFile,FileFormfileForm){
if(imgFile!=null&&imgFile.getFileSize()>0){
StringfileName=imgFile.getFileName();
StringsqlPath="img/"+fileName;
//圖片所在路徑
StringsavePath=request.getSession().getServletContext().getRealPath("/")+"img\"+fileName;
System.out.println(fileName);
System.out.println(sqlPath);
System.out.println(savePath);
HttpSessionsession=request.getSession();
session.setAttribute("savePath",savePath);
session.setMaxInactiveInterval(60*60);
//StringsavePath1=(String)session.getAttribute("savePath");
//資料庫
fileForm.getFile().setFileEmpPhoto(sqlPath);
//文件
try{
InputStreaminput=imgFile.getInputStream();
FileOutputStreamoutput=newFileOutputStream(savePath);
byte[]b=newbyte[1024];
while(input.read(b)!=-1){
output.write(b);
b=newbyte[1024];
}
output.close();
input.close();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}

2. Java中如何改變參數的值(實現按址傳遞)

java方法中傳值和傳引用的問題是個基本問題,但是也有很多人一時弄不清。你的題目本身就有歧義,無法直接針對題目作答(因為java不存在類似c或c++那樣的傳址)。我從頭講起:
(一)基本數據類型:傳值,方法不會改變實參的值。
(二)對象類型參數:傳引用,方法體內改變形參引用,不會改變實參的引用,但有可能改變實參對象的屬性值。例如
public class TestFun4 {
public static void testStringBuffer(StringBuffer sb){
sb.append("java");//改變了實參的內容
}
public static void main(String[] args) {
StringBuffer sb= new StringBuffer("my ");
new TestFun4().testStringBuffer(sb);
System.out.println("sb="+sb.toString());//內容變化了
}
}

3. 怎麼用java程序實現上傳文件到指定的URL地址

代碼如下:

import java.io.*;
/**
* 復制文件夾或文件夾
*/
public class CopyDirectory {
// 源文件夾
static String url1 = "f:/photos";
// 目標文件夾
static String url2 = "d:/tempPhotos";
public static void main(String args[]) throws IOException {
// 創建目標文件夾
(new File(url2)).mkdirs();
// 獲取源文件夾當前下的文件或目錄
File[] file = (new File(url1)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
// 復制文件
File(file[i],new File(url2+file[i].getName()));
}
if (file[i].isDirectory()) {
// 復制目錄
String sourceDir=url1+File.separator+file[i].getName();
String targetDir=url2+File.separator+file[i].getName();
Directiory(sourceDir, targetDir);
}
}
}
// 復制文件
public static void File(File sourceFile,File targetFile)
throws IOException{
// 新建文件輸入流並對它進行緩沖
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff=new BufferedInputStream(input);

// 新建文件輸出流並對它進行緩沖
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff=new BufferedOutputStream(output);

// 緩沖數組
byte[] b = new byte[1024 * 5];
int len;
while ((len =inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此緩沖的輸出流
outBuff.flush();

//關閉流
inBuff.close();
outBuff.close();
output.close();
input.close();
}
// 復制文件夾
public static void Directiory(String sourceDir, String targetDir)
throws IOException {
// 新建目標目錄
(new File(targetDir)).mkdirs();
// 獲取源文件夾當前下的文件或目錄
File[] file = (new File(sourceDir)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
// 源文件
File sourceFile=file[i];
// 目標文件
File targetFile=new
File(new File(targetDir).getAbsolutePath()
+File.separator+file[i].getName());
File(sourceFile,targetFile);
}
if (file[i].isDirectory()) {
// 准備復制的源文件夾
String dir1=sourceDir + "/" + file[i].getName();
// 准備復制的目標文件夾
String dir2=targetDir + "/"+ file[i].getName();
Directiory(dir1, dir2);
}
}
}
}

熱點內容
我的世界國服pvp伺服器ip地址 發布:2025-08-17 19:45:35 瀏覽:497
聊城電腦伺服器 發布:2025-08-17 19:34:59 瀏覽:407
互聯網編程語言 發布:2025-08-17 19:18:40 瀏覽:851
python主流框架 發布:2025-08-17 19:11:51 瀏覽:176
開源海量文件存儲 發布:2025-08-17 19:07:05 瀏覽:193
帶密碼的發票有什麼用 發布:2025-08-17 18:53:18 瀏覽:689
免費php模板下載 發布:2025-08-17 18:47:31 瀏覽:240
ubuntuphp開發 發布:2025-08-17 18:34:44 瀏覽:499
c語言小程序游戲 發布:2025-08-17 18:23:09 瀏覽:801
ios今日頭條源碼 發布:2025-08-17 18:23:02 瀏覽:311