
正文
java文件路径代码 java的路径怎么设置
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java中怎么把文件上传到服务器的指定路径?
文件从本地到服务器java文件路径代码的功能java文件路径代码,其实是为了解决目前浏览器不支持获取本地文件全路径。不得已而想到上传到服务器的固定目录,从而方便项目获取文件,进而使程序支持EXCEL批量导入数据。
java中文件上传到服务器的指定路径的代码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中获取前台传来数据并保存
/**
* 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转化为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();
}
相关问答
Q1: java中怎么写文件路径
第一个 : "C:\\mydoc\\aa.doc" , 这个用双斜线
第二个 : "C:/mydoc/aa.doc" ,这个单斜线就行了
我建议java文件路径代码你用
String path = "C:"+File.separator+"my.doc" ;
System.out.println(path);
File.separator 这是用java文件路径代码你所用java文件路径代码的系统默认的文件分割符java文件路径代码,,
Q2: 谁告诉我java中 读取文件时想用相对路径,代码怎么写?
1绝对路径:加上盘符,就是绝对正确的地址,一般通过我的电脑打开到那个位置,复制地址就可以。
2相对路径:
a;同级目录: 直接填写文件名称;
b;下级链接:带上文件夹,写上文件名称;
c:上级链接:"..\文件名称"
Q3: 关于java代码中文件路径的问题
这就是相对路径
指的是相对于工程文件的位置而言
在eclipse的结构图中的位置
在windows的文件夹里的位置
在查看属性里的绝对路径的位置
代码来找文件路径
public class Test {
public static void main(String[] args) throws Exception {
System.out.println("当前目录的路径\t"+new File(".").getCanonicalPath());// "."表示当前目录
File file = new File("Buffered.txt");
if(!file.exists()){//如果不存在,就新建该文件
file.createNewFile();
}
System.out.println("Buffered.txt的绝对路径\t"+file.getCanonicalPath());
System.out.println("Buffered.txt的相对路径\t"+file.getPath());
}
}
输出
当前目录的路径 D:\space\workspace\Demo
Buffered.txt的绝对路径 D:\space\workspace\Demo\Buffered.txt
Buffered.txt的相对路径 Buffered.txt
Q4: java中 读取文件时想用相对路径,代码怎么写?
test
|
src
|
t090417
|
test.properties
Read.java
test.properties:
TEST=test
Read.java:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class Read {
public static String TEST ;
private static Properties loadPropertyFile() throws FileNotFoundException,IOException{
Properties properties = new Properties() ;
FileInputStream fs = new FileInputStream("src/t090417/test.properties");
properties.load(fs);
return properties ;
}
public static void loadProperty(){
try{
Properties properties = loadPropertyFile();
TEST = properties.getProperty("TEST");
System.out.println("read from properties: "+TEST);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
loadProperty();
}
}
其中用的就是相对路径!
java文件路径代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java的路径怎么设置、java文件路径代码的信息别忘了在本站进行查找喔。








