网络服务器有保存文件,将收到的图片保存到网络服务器上的文件夹

对于类似的功能(从装载Android的照片与Servlet),这里的Android客户端的代码,我使用(在这里发帖而稍加编辑):

URI uri = URI.create(// path to file);

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);

// several key-value pairs to describe the data, one should be filename

entity.addPart("key", new StringBody("value"));

File inputFile = new File(photoUri.getPath());

// optionally reduces the size of the photo (you can replace with FileInputStream)

InputStream photoInput = getSizedPhotoInputStream(photoUri);

entity.addPart("CONTENT", new InputStreamBody(photoInput, inputFile.getName()));

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(uri);

HttpContext localContext = new BasicHttpContext();

httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost, localContext);

和这里的代码来接受它。首先,一定要标记您的servlet类为支持多消息:

@MultipartConfig

public class PhotosServlet extends HttpServlet

然后身体的相关部分:

HttpEntity entity = new InputStreamEntity(request.getPart("CONTENT").getInputStream(), contentLength);

InputStream inputFile = entity.getContent();

// string extension comes from one of the key-value pairs

String extension = request.getParameter(//filename key);

// first write file to a file

File images = new File(getServletContext().getRealPath("images"));

File filePath = File.createTempFile("user", extension, images);

writeInputDataToOutputFile(inputFile, filePath); // just copy input stream to output stream

String path = filePath.getPath();

logger.debug("Wrote new file, filename: " + path);

希望它能帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值