Java上传下载

1 篇文章 0 订阅
利用java Cos下载

               String filepath=getRequest().getRealPath("excelpath");//文件绝对路径
	        String filename=getRequest().getParameter("excel");//获得单个文件名
	        String guessCharset="gbk";
	        String path=filepath+"/"+filename;
		    try { //使用iso8559_1编码方式
		        String isofilename = new String(filename.getBytes(guessCharset),
		                             "ISO-8859-1");
		        getResponse().setContentType("application/octet-stream");
		        getResponse().setHeader("Content-Disposition",
		                            "attachment; filename=" + filename);
		        ServletOutputStream out = null;
		        out = getResponse().getOutputStream();
		        ServletUtils.returnFile(path,out);//下载文件
		    }
		    catch(UnsupportedEncodingException ex) {//iso8559_1编码异常
//		        ex.printStackTrace();
		    }catch(IOException e){//getOutputStream()异常。
//		        e.printStackTrace();
		    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 上传下载文件可以使用 Java 的 IO 流和网络编程相关的类库来实现。以下是一个简单的示例代码: 文件上传: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class FileUploader { public static void main(String[] args) throws IOException { File file = new File("path/to/file"); URL url = new URL("http://example.com/upload"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); String boundary = "---------------------------" + System.currentTimeMillis(); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); try (InputStream inputStream = new FileInputStream(file)) { String header = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + "Content-Type: " + URLConnection.guessContentTypeFromName(file.getName()) + "\r\n\r\n"; byte[] headerBytes = header.getBytes("UTF-8"); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) > 0) { connection.getOutputStream().write(buffer, 0, bytesRead); } byte[] footerBytes = ("\r\n--" + boundary + "--\r\n").getBytes("UTF-8"); connection.getOutputStream().write(footerBytes); } try (InputStream inputStream = connection.getInputStream()) { // Handle response from server } } } ``` 文件下载: ```java import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class FileDownloader { public static void main(String[] args) throws IOException { URL url = new URL("http://example.com/file"); URLConnection connection = url.openConnection(); try (InputStream inputStream = connection.getInputStream(); FileOutputStream outputStream = new FileOutputStream("path/to/file")) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, bytesRead); } } } } ``` 注意:以上示例代码中的 URL 和 boundary 都需要替换为实际使用的值。另外,文件上传的示例代码中需要设置连接的 doOutput 和 Content-Type 属性,以及手动拼接 multipart/form-data 的请求体。如果使用第三方库,可能会更方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值