Java文件上传获取不到客户端文件
自己写了一个上传类,在本地计算机上传文件可以,但是放到服务器上面就不行了,找不到客户端选定的文件,路径可以找到。JSP代码:test1.jsp<formaction="t...
自己写了一个上传类,在本地计算机上传文件可以,但是放到服务器上面就不行了,找不到客户端选定的文件,路径可以找到。
JSP代码:
test1.jsp
<form action="test2.jsp" name="form1">
<div>
<input type="file" name="my-file" id="my-file" onchange="inputFileOnChange();" />
<input type="submit" name="sub" value="sub">
</div>
</form>
=============================================================
test2.jsp
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//String filePath=request.getParameter("p_img");
String filePath=request.getParameter("my-file");
UploadImg uimg=new UploadImg();
System.out.println(filePath);
String sPath=application.getRealPath("");
boolean bool=uimg.upImg(filePath,sPath);
%>
================================================================
Java源代码:
public class UploadImg {
public boolean upImg(){
return false;
}
public boolean upImg(String fromPath,String serverPaht){
System.out.println("方法中的用户文件路径="+fromPath);
String fileName=fromPath.substring(fromPath.lastIndexOf(File.separator)+1);
String savePath=serverPaht+File.separator+"ProductIMG";//文件保存目录
System.out.println(savePath);
try{
File file=new File(savePath);
if(!file.exists()){
file.mkdir();
} //文件输入流
FileInputStream fis=new FileInputStream(fromPath);
BufferedInputStream bis=new BufferedInputStream(fis);
byte [] content=new byte[255];
//文件输出流
File j=new File(savePath+File.separator+fileName);
FileOutputStream fos=new FileOutputStream(j);
BufferedOutputStream bos=new BufferedOutputStream(fos);
while(bis.read(content)!=-1){//写入文件
bos.write(content);
bos.flush();
}
bis.close();
bos.close();
}catch(Exception e){
e.printStackTrace();
}
return true;
}
}
===================================================
错误:
java.io.FileNotFoundException: E:\CHEM SPEC\azoty pa6(2).jpg (系统找不到指定的路径。)
nanayask 这个应该是怎么写呢?
yxb88858 我就是想自己学习,写这个功能~ 老用别人的也不好 展开
JSP代码:
test1.jsp
<form action="test2.jsp" name="form1">
<div>
<input type="file" name="my-file" id="my-file" onchange="inputFileOnChange();" />
<input type="submit" name="sub" value="sub">
</div>
</form>
=============================================================
test2.jsp
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//String filePath=request.getParameter("p_img");
String filePath=request.getParameter("my-file");
UploadImg uimg=new UploadImg();
System.out.println(filePath);
String sPath=application.getRealPath("");
boolean bool=uimg.upImg(filePath,sPath);
%>
================================================================
Java源代码:
public class UploadImg {
public boolean upImg(){
return false;
}
public boolean upImg(String fromPath,String serverPaht){
System.out.println("方法中的用户文件路径="+fromPath);
String fileName=fromPath.substring(fromPath.lastIndexOf(File.separator)+1);
String savePath=serverPaht+File.separator+"ProductIMG";//文件保存目录
System.out.println(savePath);
try{
File file=new File(savePath);
if(!file.exists()){
file.mkdir();
} //文件输入流
FileInputStream fis=new FileInputStream(fromPath);
BufferedInputStream bis=new BufferedInputStream(fis);
byte [] content=new byte[255];
//文件输出流
File j=new File(savePath+File.separator+fileName);
FileOutputStream fos=new FileOutputStream(j);
BufferedOutputStream bos=new BufferedOutputStream(fos);
while(bis.read(content)!=-1){//写入文件
bos.write(content);
bos.flush();
}
bis.close();
bos.close();
}catch(Exception e){
e.printStackTrace();
}
return true;
}
}
===================================================
错误:
java.io.FileNotFoundException: E:\CHEM SPEC\azoty pa6(2).jpg (系统找不到指定的路径。)
nanayask 这个应该是怎么写呢?
yxb88858 我就是想自己学习,写这个功能~ 老用别人的也不好 展开
5个回答
展开全部
必须要现判断文件路径是否存在的,否则需要创建个文件,没这个文件,肯定会报错。
/**
* 加密文件
*
* @param fileName
* @param date
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String encodeAESFileUploadByFtp(String plainFilePath, String fileName, String date,String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上传文件开始");
byte[] bytes = encodeAES(key, bos.toByteArray());
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
Log.info("连接远程上传服务器"+CMBCUtil.CMBCHOSTNAME+":"+2021);
ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
// Log.info("连接远程上传服务器"+"192.168.54.106:"+2021);
// ftpClient.connect("192.168.54.106", 2021);
// ftpClient.login("hkrt-CMBCHK", "3OLJheziiKnkVcu7Sigz");
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
ftpClient.makeDirectory(date);
ftpClient.changeWorkingDirectory("/"+filepath+"/" + date);
}
}
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, is);
Log.info("加密上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/" + date);
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
/**
* 解密文件
*
* @param fileName
* @param date
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String encodeAESFileDownloadByFtp(String plainFilePath, String fileName, String date,String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
// Log.info("下载并解密文件开始");
Log.info("连接远程下载服务器"+CMBCUtil.CMBCHOSTNAME+":"+2021);
ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
ftpClient.changeWorkingDirectory("/"+filepath+"/");
}
}
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(date)) {
bl = "true";
ftpClient.changeWorkingDirectory("/"+filepath+"/" + date);
}
}
Log.info("检查文件路径是否存在:/"+filepath+"/"+date + " "+ bl);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath+"/" + date);
return bl;
}
bl = "false";
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true";
Log.info("下载并解密文件开始。");
fos = new FileOutputStream(plainFilePath);
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(fileName);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
byte[] bytes = decodeAES(key, bos.toByteArray());
fos = new FileOutputStream(plainFilePath);
fos.write(bytes);
Log.info("下载并解密文件结束:"+plainFilePath);
}
}
Log.info("检查文件是否存:"+fileName+" "+bl);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。");
return bl;
}
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
// 调用样例:
public static void main(String[] args) {
try {
// 密钥
// 明文文件路径
String plainFilePath = "D:/req_20150204_0011.txt";
// 密文文件路径
String secretFilePath = "req_20150204_00134.txt";
// 加密
// encodeAESFile(key, plainFilePath, secretFilePath);
// 解密
plainFilePath = "D:/123.sql";
// secretFilePath = "D:/test11111.sql";
// decodeAESFile(key, plainFilePath, secretFilePath);
} catch (Exception e) {
e.printStackTrace();
}
/**
* 加密文件
*
* @param fileName
* @param date
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String encodeAESFileUploadByFtp(String plainFilePath, String fileName, String date,String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上传文件开始");
byte[] bytes = encodeAES(key, bos.toByteArray());
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
Log.info("连接远程上传服务器"+CMBCUtil.CMBCHOSTNAME+":"+2021);
ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
// Log.info("连接远程上传服务器"+"192.168.54.106:"+2021);
// ftpClient.connect("192.168.54.106", 2021);
// ftpClient.login("hkrt-CMBCHK", "3OLJheziiKnkVcu7Sigz");
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
ftpClient.makeDirectory(date);
ftpClient.changeWorkingDirectory("/"+filepath+"/" + date);
}
}
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, is);
Log.info("加密上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/" + date);
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
/**
* 解密文件
*
* @param fileName
* @param date
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String encodeAESFileDownloadByFtp(String plainFilePath, String fileName, String date,String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
// Log.info("下载并解密文件开始");
Log.info("连接远程下载服务器"+CMBCUtil.CMBCHOSTNAME+":"+2021);
ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
ftpClient.changeWorkingDirectory("/"+filepath+"/");
}
}
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(date)) {
bl = "true";
ftpClient.changeWorkingDirectory("/"+filepath+"/" + date);
}
}
Log.info("检查文件路径是否存在:/"+filepath+"/"+date + " "+ bl);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath+"/" + date);
return bl;
}
bl = "false";
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true";
Log.info("下载并解密文件开始。");
fos = new FileOutputStream(plainFilePath);
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(fileName);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
byte[] bytes = decodeAES(key, bos.toByteArray());
fos = new FileOutputStream(plainFilePath);
fos.write(bytes);
Log.info("下载并解密文件结束:"+plainFilePath);
}
}
Log.info("检查文件是否存:"+fileName+" "+bl);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。");
return bl;
}
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
// 调用样例:
public static void main(String[] args) {
try {
// 密钥
// 明文文件路径
String plainFilePath = "D:/req_20150204_0011.txt";
// 密文文件路径
String secretFilePath = "req_20150204_00134.txt";
// 加密
// encodeAESFile(key, plainFilePath, secretFilePath);
// 解密
plainFilePath = "D:/123.sql";
// secretFilePath = "D:/test11111.sql";
// decodeAESFile(key, plainFilePath, secretFilePath);
} catch (Exception e) {
e.printStackTrace();
}
展开全部
你好像理会错了点东西。
上传是从客户端读取文件,通过流传输。服务器接收数据,并写入文件。
所以FileInputStream fis=new FileInputStream(fromPath);
这样的代码根本就有问题。
上传是从客户端读取文件,通过流传输。服务器接收数据,并写入文件。
所以FileInputStream fis=new FileInputStream(fromPath);
这样的代码根本就有问题。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package action;
import java.io.*;
public class UpFileAction{
private String filePathName;
static public final String ROOTPATH="D:\\";
static public final int bufferSize=1024;
static public final String SUCCESS="yes";
public String shangchuan(){
try{
filePathName= new String(filePathName.getBytes("ISO8859-1"),"utf-8"); //将传进来的值中??转为汉字
File file=new File(filePathName);
if(file.exists()==false) {System.out.println("文件不存在");return "error";}
String newFilename=file.getName();
/*
//取当前日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
String date=sdf.format(calendar.getTime());
//10亿的随机数
java.util.Random r=new java.util.Random();
String radom=String.valueOf(r.nextInt(1000000000));
fileFileName=date+radom+"."+lastname;
*/
InputStream is=new FileInputStream(file);
File newFile=new File(ROOTPATH+newFilename);
OutputStream os=new FileOutputStream(newFile);
byte[] buffer=new byte[bufferSize];
int length=0;
while( (length=is.read(buffer))>0){
os.write(buffer, 0, length);
}
is.close();
os.close();
}catch(Exception e){
System.out.println("UpFile出错:");
e.printStackTrace();
return "error";
}
return SUCCESS;
}
}
如果不介意 :
看我的这个文件上传
import java.io.*;
public class UpFileAction{
private String filePathName;
static public final String ROOTPATH="D:\\";
static public final int bufferSize=1024;
static public final String SUCCESS="yes";
public String shangchuan(){
try{
filePathName= new String(filePathName.getBytes("ISO8859-1"),"utf-8"); //将传进来的值中??转为汉字
File file=new File(filePathName);
if(file.exists()==false) {System.out.println("文件不存在");return "error";}
String newFilename=file.getName();
/*
//取当前日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
String date=sdf.format(calendar.getTime());
//10亿的随机数
java.util.Random r=new java.util.Random();
String radom=String.valueOf(r.nextInt(1000000000));
fileFileName=date+radom+"."+lastname;
*/
InputStream is=new FileInputStream(file);
File newFile=new File(ROOTPATH+newFilename);
OutputStream os=new FileOutputStream(newFile);
byte[] buffer=new byte[bufferSize];
int length=0;
while( (length=is.read(buffer))>0){
os.write(buffer, 0, length);
}
is.close();
os.close();
}catch(Exception e){
System.out.println("UpFile出错:");
e.printStackTrace();
return "error";
}
return SUCCESS;
}
}
如果不介意 :
看我的这个文件上传
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JSP中获取文件流,设进request的输入流里,后台获得流信息。。。保存,貌似你的过程有问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
到网上找一个上传文件的Jar包吧,做的非常好,你的代码根本就是错!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询