java文件下载的问题,还没解决,
java文件下载,我这里用得的是一个Apache FtpClient jar包,遇到的问题,如下:
1:上传图片,到服务器上面,打开花面,原来是没用二进制进行传输
修改为 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 这样上传就能解决图片花面
为什么选择用Ftp上传呢, 平常我们web页面 都是Apache fileupload 类,但是apache fileupLoad 只能在web应用 传输,用ftp,可以传输在不同的主机地址,只要主机上,搭建 了Ftp服务器,
if(isMult==true) //说明是
{
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
{
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
FTPClient ftpClient=new FTPClient();
ftpClient.connect("127.0.0.1", 2121);
ftpClient.login("admin", "admin");
out.print(ftpClient.getRemotePort());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GBK");
ftpClient.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
//out.println(ftpClient.getReplyCode());
//设置上传工作目录 不设置,默认 res\home
//ftpClient.changeWorkingDirectory("/");
// FileInputStream fileInputStream=new FileInputStream(fullFile);
String newFileName="test_"+new Random().nextInt(10000)+".jpeg";
ftpClient.storeFile(newFileName,item.getInputStream());
ftpClient.connect("127.0.0.1", 2121);
ftpClient.login("admin", "admin");
out.print(ftpClient.getRemotePort());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GBK");
ftpClient.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
//out.println(ftpClient.getReplyCode());
//设置上传工作目录 不设置,默认 res\home
//ftpClient.changeWorkingDirectory("/");
// FileInputStream fileInputStream=new FileInputStream(fullFile);
String newFileName="test_"+new Random().nextInt(10000)+".jpeg";
ftpClient.storeFile(newFileName,item.getInputStream());
2:图片下载
下载的问题还没解决
现象:图片下载到本机,打开的图片是空白,不知道是什么原因
response.setContentType("application/x-msdownload");
//设置弹出对话框 为下载对话框
response.setHeader("Content-Disposition", "attachment;filename=\"" + file_name + "\"" );
// 通过输入输出流 下载 文件
java.io.InputStream input =new FileInputStream(ff);
byte r[]=new byte[1024];
OutputStream outputStream=response.getOutputStream();
byte r[]=new byte[1024];
OutputStream outputStream=response.getOutputStream();
int c;
while((c=input.read(r))!=-1)
{
outputStream.write(r, 0, c);
}
outputStream.flush();
input.close();
outputStream.close();
while((c=input.read(r))!=-1)
{
outputStream.write(r, 0, c);
}
outputStream.flush();
input.close();
outputStream.close();
下载下来,就是空白,但是文件大小一样大,
但是去掉 response.setHeader()那个 弹出对话框 为下载对话框, 图片文件,就没问题,奇怪啊,先记在这里